字符串
脏数据
ctrl ##help工具
定义
a = 'deqw'
a = "eew'qq"
a = """
用户管理系统
1.添加用户
2.删除用户
3.显示用户
....
"""
特性
索引从0开始
[0] ##第一个数字
[-1]/[最后一个数字] ##表示最后一个字符
[0:3] ##012
[0:4:2] ##02
[:] ##所有
[:3] ##前三个
[::-1] ##反转
[2:] ##前两个不显示(除了第二个字符之外的其他全部字符)
[s * 3] ##重复
print('hello ' + 'python') ##连接
print('aa' in s)
print('he' not in s) ##成员操作符
for i in s:
print(i) ##or循环遍历,取出每一个字符
方法
title—— s.title() ##大写s每一个单词的第一个字母
capitalize—— s.capitalize() ##大写s第一个单词的第一个字母
upper—— s.upper() ##将s转换为大写
lower—— s.lower() ##将s转换为xiao写
s.istitle() ##判断首字母是否大写
s.isupper() ##判断是不是大写
s.islower() ##判断是不是小写
s.endswith('.log') ##判断是否以.log结尾
s.startswith('http://') ##判断是否以http://开头
s.lstrip() ##消除s的左空白
s.rstrip('h') ##消除s右边的h
s.strip() ##消除s前后的空白
s.isdigit() ##s是否是纯数字
s.isalpha() ##s是否是字母
s.isalnum() ##s是否是字母或数字
对齐
print('学生管理系统'.center(30)) ##居中
print('学生管理系统'.center(30,'@')) ##居中,并且两边用@填充
print('学生管理系统'.ljust(30,'#')) ##左对齐,用#填充
print('学生管理系统'.rjust(30,'#')) ##右对齐,用#填充
替换
s.find('hello') ##找到子字符串(hello),并返回最小的索引
s.rfind('hello') ##找到子字符串(hello),并返回最大的索引
s.replace('hello','redhat') ##替换字符串中的hello为redhat
统计
'hello'.count('l') ##统计hello中有几个l
len('wfqfqfqfq') ##统计wfqfqfqfq有几个字符
分离和连接
s.split('.') ##以.为界线分割字符串s
'/'.join(date1) ##将可迭代对象date1以/连接,转化为字符串
'~~'.join('hello') ##将hello的每个单词用~~隔开