字符串常见操作
获取长度:
len()
查找内容:
find,index,rfind,rindex
find:从左向右查找,只要遇到一个符合要求的则返回其下标位置,如果没有找到则返回-1例如:
path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg' i = path.find('_') i2 = path.find('.jpg') #find返回的是当前字符串所在下标,所以要下标+1 print(path[i+1:i2])输出:
scssfqwewqewqqw123wdqe12ew1
rfind:right find,从右向左查找,只要遇到一个符合要求的则返回其下标位置,如果没有找到则返回-1例如:
path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg' i = path.rfind('.') print(path[i:])输出:
.jpg
index:和find类似,从左往右查找指定字符串,找到则返回其下标位置,但是找不到会报错
rindex:和rfind类似,从右往左查找指定字符串,找到则返回其下标位置,但是找不到会报错
判断:
startswith、endswith、isalpha、isdigit、isalnum、isspace、isupper判断字符串返回值都是布尔类型(True、False)
startswith:判断是否以指定字符串开头s = 'dong_scssfqwewqewqqw123wdqe12ew1.jpg' result = s.startswith('abc') print(result)输出:
False
endswith:判断是否以指定字符串结尾s = 'dong_scssfqwewqewqqw123wdqe12ew1.jpg' result = s.endswith('.jpg') print(result)输出:
True
isalpha:判断是否字母s = 'qwEsdnDi' result = s.isalpha() print(result) #输出: True
isdigit:判断是否数字s = '1321864' result = s.isdigit() print(result) #输出 True
isalnum:判断是否数字或字母s = '123abc' result = s.isalnum() print(result) #输出 True
isspace:判断是否全空格s = ' ' result = s.isspace() print(result) #输出 True
issupper:判断是否全大写s = 'QWESAcvv' result = s.isupper() print(result) #输出 False
islower:判断是否全小写s = 'smocn' result = s.islower() print(result) #输出 True注意:都是判断的字符串
计算出现次数:
count
count:计算字符串出现的次数path = 'https://www.baidu.com/img/dong_scssfqwewqewqqw123wdqe12ew1.jpg' n = path.count('.') print(n) #输出 3
替换内容:
replace
replace:替换内容,replace('old str','new str',count),count代表替换次数,默认是全部替换。可以用正则表达式替换s = '王二麻子说:李四你去唱歌,张三去跳舞吧!' result = s.replace('李四','老王',1) #1表示替换1次 print(result) #输出 王二麻子说:老王你去唱歌,张三去跳舞吧!
切割字符串:
split、resplit、splitlines、partition、repartition
split:split('分隔符',maxsplit),返回结果是一个列表,maxsplit表示最多分割次数s = '张三,李四,王五,尼古拉斯大熊,伊丽莎白静香' result = s.split(',',2) #不写分割次数默认全分割 print(result) #输出 ['张三', '李四', '王五,尼古拉斯大熊,伊丽莎白静香'] #split分割成一个的列表,python里面叫做列表。
rsplit:同split,rsplit只是从右到左分割
splitlines:同split,只不过是按行切割s = '''王二麻子你去学习吧! 张三你去跳舞吧! 李四你去唱歌吧! 王五你去吃饭吧!''' result = s.splitlines() print(result) #输出 ['王二麻子你去学习吧!','张三你去跳舞吧!','李四你去唱歌吧!','王五你去吃饭吧!']
partition:分隔符也带入s = '王八犊子,麻烦你圆润的走开!' result = s.partition(',') print(result) #输出: ('王八犊子', ',', '麻烦你圆润的走开!')
修改大小写:
capitalize,title,upper,lower
title:每个单词首字母大写s = 'HELLO WORD!' result = s.title() print(result) #输出 Hello Word!
upper:所有字母转换成大写字母,数字不影响s = 'hello world1' result = s.upper() print(result) #输出 HELLO WORLD1
lower:所有字母转换成小写字母,数字不影响s = 'HELLO world123' result = s.lower() print(result) #输出: hello world123
capitalize:一句话的第一个字母大写,其他字母统统转换成小写。s = 'hello WORLd' result = s.capitalize() print(result) #输出: Hello,world
空格处理:
ljust,rjust,center:添加空格控制字符串的对齐方式。
lstrip,strip,rstrip:删除字符串左侧或者右侧的空格
strip:删除字符串的头尾空格,返回一个字符串s = 'admin ' print(len(s)) result = s.strip() print(len(result)) #输出 8 5
rstrip:只去除右侧空格s = ' admin ' print(len(s)) result = s.rstrip() print(len(result)) print(result) #输出 12 8 admin
lstrip:只去除左侧空格s = ' admin ' result = s.lstrip() print(len(s)) print(len(result)) #输出 11 8
center:给个整体字符长度,输出的字符串位于整体字符的中间位置s = 'hello word!' result = s.center(30) print(result) #输出: hello word!
ljust:给出整体字符长度,输出的字符串位于整体字符的左侧位置s = 'hello word!' result = s.ljust(30) print(result) print(len(result)) #输出: hello word! 30
rjust:给出整体字符长度,输出的字符串位于整体字符的右侧位置s = 'hello word!' result = s.rjust(30) print(result) #输出: hello word!
字符串拼接:
joinjoin:将整个join中的字符,元组,列表一个个取出,并在后面添加指定的字符,join中最后的字符保留,不加指定字符
s = ',' a = '。' b = '、' result = s.join(['hello', 'world', '我是李四!']) print(result) result = a.join(['hello', 'world', '我是李四!']) print(result) result = b.join(['hello', 'world', '我是李四!']) print(result) #输出: hello,world,我是李四! hello。world。我是李四! hello、world、我是李四!注:取出列表中的字符串,并在每个字符串后面将s中的字符进行拼接,最后的字符串不拼接。
字符串格式化
%d %s %f…s = '您好,我是王五!' f = 666 d = 123 print('王五说:%s' % s) print('王五说:%f' % f) print('王五说:%d' % d) #输出: 王五说:您好,我是王五! 王五说:666.000000 王五说:123
format:通过字符串中的{}来识别替换字段,从而完成字符串的格式化省略字段名方法:
name = '老六' age = 66 result = '{}今年{}岁'.format(name,age) print(result) #输出 老六今年66岁数字字段名:数字填充
name = '老六' age = 66 result = '{0}今年{1}岁,老王也是{1}岁。'.format(name,age) print(result) #输出 老六今年66岁,老王也是66岁。变量字段名:
format的参数必须是关键字参数
关键字参数:必须直接指定变量及变量参数。result = '{name}今年{age}岁,老王也是{age}岁。'.format(name = '老六',age = 66) print(result) #输出 老六今年66岁,老王也是66岁。
注:在python中,字符串是不可变的!所有字符串相关方法,都不会改变原有字符串,都是返回一个结果,在这个新的返回值里,保留了执行后的结果。
扩展:生成随机的数字字母组合的6位随机数
import random
#这是导入random的函数
fileName = ''
s = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789'
for i in range(6): #range(6)i取到从0-6的数字,也就是循环6次
index = random.randint(0,len(s)-1)
fileName += s[index]
print(fileName)
#输出:
随机六位数数字和大小写字母组合数
1万+

被折叠的 条评论
为什么被折叠?



