2.3. 修改切片方向
print(s1[1:5:1]) # 输出 ello 默认从1开始 差值为1
print(s1[1:5:2]) # 输出 el 默认从1开始 差值为2
print(s1[-1:-5:-1]) # 反方向的种 从后面开始 差值1 索引到4
print(s1[:]) # 不写数字就默认都要
print(s1[2:]) # 从索引2开始往后都要
print(s1[:5]) # 从索引0开始往后要到4
print(s1[::2]) # 索引所有值 差值2
2.4.统计字符串中字符的个数
print(len(s1)) # 12 总共有12个数
2.5.移除字符串首尾指定的字符
username = input('username>>>:').strip()
# username = input('username>>>:')
# username = username.strip() 两行代码和上面一样代码运行结果一样 单代码更方便!
if username == 'wei': # 如果后面没有strip(),'wei ' 如果有空字符串,输出有误!
print('登陆成功')
res = ' jason '
print(len(res)) # 空字符串也占位1 长度9个字符
print(len(res.strip())) # strip()括号内不写 默认移除首尾的空格 结果:5
res1 = '$$wei$$'
print(res1.strip('$')) # wei
print(res1.lstrip('$')) # wei$$
print(res1.rstrip('$')) # $$wei
2.6.切割字符串中指定的字符
res = 'jason|123|read'
print(res.split('|')) # ['jason', '123', 'read'] 该方法的处理结果是一个列表
# split翻译:
# V:使....分裂/n:分裂; 分离; 分歧; 划分; 分别; 份额; 裂缝; 裂口; 劈叉
name, password, hobby = res.split('|') # 使用变量名代替对应的字符
print(res.split('|', maxsplit=1)) # maxsplit:最大切割值 后面数字多少 切割多少
# ['jason', '123|read'] 默认从左往右切指定个数
print(res.rsplit('|',maxsplit=1))
# ['jason|123', 'read'] 从右往左切指定个数
resplit:从右到左
2.7.字符串格式化输出
format玩法1:等价于占位符
res = 'my name is {} my age is {}'.format('wei', 123) # {}等价于占位符
print(res) # my name is wei my age is 123
format玩法2:索引取值并支持反复使用
res = 'my name is {0} my age is {1} {0} {0} {1}'.format('jason', 123)
print(res) # my name is jason my age is 123 jason jason 123
format玩法3:占位符见名知意
res = 'my name is {name1} my age is {age1}'.format(name1='jason', age1=123)
print(res) # my name is jason my age is 123 jason 123 jason
format玩法4:推荐使用(******* 给鸡哥拉满!!!!!!)
name = input('username>>>:')
age = input('age>>>:')
res = f'my name is {name} my age is {age}' # f代表了 .format
print(res)
3.字符串
1.大小写相关
res = 'hElLO WorlD 666'
print(res.upper()) # HELLO WORLD 666 全部大写
print(res.lower()) # hello world 666 字母全部小写
'''
图片验证码:生成没有大小写统一的验证码 展示给用户看
获取用户输入的验证码 将用户输入的验证码和当初产生的验证码统一转大写或者小写再比对
'''
code = 'zBcqD1'
print('请输入图中的验证码的内容',code)
cs_code = input('请输入验证码>>>:').strip()
if cs_code.upper() == code.upper(): # upper:全部大写 lower:英文小写
print('验证码正确')
3.1 判断字符串中的大小写
res = 'hello world'
print(res.isupper()) # 判断字符串是否是纯大写 False
print(res.islower()) # 判断字符串是否是纯小写 True
3.2 判断字符串是否是纯数字
res = ''
print(res.isdigit()) # sdigit是python的一个函数,主要用于检查是否为数字 False
guess_age = input('guess_age>>>:').strip() # 获取用户输入值
if guess_age.isdigit(): # 判断用户输入值是否是数字
guess_age = int(guess_age) # 如果是
print('年龄', guess_age) # 打印年龄
else:
print('代笔!年龄都不知道怎么输入啊??'))
3.3替换字符串中指定的内容
res = 'my name is jason jason jason jason jason'
print(res.replace('jason', '你想要输入的')) # 前面不变 后面更改
# my name is handsome handsome handsome handsome handsome(输出结果)
print(res.replace('jason', 'tonySB', 1))
# my name is tonySB jason jason jason jason 从左到右替换指定内容
3.4.字符串的拼接
ss1 = 'hello'
ss2 = 'world'
print(ss1 + '$$$' + ss2) # hello$$$world
print(ss1 * 10) # 10遍hello
print('|'.join(['jason', '123', 'read', 'JDB'])) # jason|123|read|JDB 加入到每个字符串
print('|'.join(['jason', 123])) #运行失败 参与拼接的数据值必须都是字符串
3.5 统计指定字符出现的次数
res = 'hello world'
print(res.count('l')) # 3 count:计数
3.6 判断字符串的开头或者结尾
res = 'jason say hello'
res = 'jason say hello'
print(res.startswith('jason')) # True startswith 开头
print(res.startswith('j')) # True
print(res.startswith('jas')) # True
print(res.startswith('a')) # False
print(res.startswith('son')) # False
print(res.startswith('say')) # False
print(res.endswith('o')) # True endswith 结尾
print(res.endswith('llo')) # True
print(res.endswith('hello')) # True
3.7.其他方法补充
res = 'helLO wORld hELlo worLD'
print(res.title()) # Hello World Hello World 每个英文首字母大写
print(res.capitalize()) # Hello world hello world 第一个英文大写
print(res.swapcase()) # HELlo WorLD HelLO WORld 相反 大写的小写 小写的大写
print(res.index('O')) # 找索引值
print(res.find('O') # 实现检索字符串并且输出运算值的意思
print(res.index('c')) # 找不到直接报错
print(res.find('c')) # 找不到默认返回 找不到默认返回-1
print(res.find('LO')) # 也是类似索引值 找到下划线地址