>>输入宽度:30
==============================
item price
______________________________
apple 0.40
Pears 0.50
find 在一个长的字符串中查找字符串,返回字符串所在位置的最左端的索引,如果没有则返回-1 str='hello world'
print(str.find('world'))
输出:6
str='hello world'
print(str.find('worldd'))
输出:-1
join 用来连接列表中的字符串 l=['1','2','3','4','5','6']
sep='+'
ret=sep.join(l)
print(ret)
输出:
1+2+3+4+5+6
lower 返回字符串的小写母版(忽略用户大小写时使用,例如,用户输入用户名含有大写字母,输入后将其转换为小写并与数据库中的保存字符匹配) str='HELLO WORLD'
print(str.lower())
输出:
hello world
replace 返回字符串中所有被匹配项被替换后的所得到的新字符串 str='HELLO WORLD'
print(str.lower().replace('world','python'))
输出:
hello python
split 按某个分隔符将字符串分割成序列,默认以空格符分割 str='1+2+3+4'
print(str.split('+'))
输出结果:
['1', '2', '3', '4']
str='HELLO WORLD'
print(str.split())
输出结果:
['HELLO', 'WORLD']
strip 去除字符串两边的空格 str=' HELLO WORLD '
print(str.strip())
输出结果:
HELLO WORLD
maketrans 创建字符映射的转换表,接收两个参数,第一个参数是字符串,表示要转换的字符串,第二个参数也是字符串表示转换的目标(两个参数是映射关系(一一对映),因此长度必须相同) intab = "el"
outtab = "EL"
trantab = str.maketrans(intab, outtab)
str = "hello world"
print (str.translate(trantab))
输出:
hELLo worLd
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx