字符串方法
Python 有一组可以在字符串上使用的内建方法。
注释:所有字符串方法都返回新值。它们不会更改原始字符串。
下面列出几个常用的 其他需要在查询
len(str) 返回字符串的长度
z='abacdklfjaslfj'
print(len(z))#14
str.strip() 去除字符串首,尾的空格
E=' hello ,world '
print(E.strip())#hello ,world
str.upper()字符串转换成大写字符串
F='hello world'
print(F.upper())#HELLO WORLD
str.lower()字符串转换成小写字符串
G='HELLO WORLD'
print(G.lower())#hello world
str.replace(str1,str2) 用str2替换掉str中的str1
H='hello world'
print(H.replace('hello','happy'))#happy world
str.split() 把字符串切割成元组;默认已空格切割
J='hello,world'
print(J.split(','))#['hello', 'world']
str1 in str 关键字in 检查 str1是否存在str中
K='hello world'
print('hello' in K) #True
str.format(st1,str2) format函数会使用传递的参数str1,str2等依次格式化字符串中的{} ;(format() 方法接受不限数量的参数,并放在各自的占位符中:)
#字符串格式str.format()
L='my name is Bill,i am {}'
print(L.format('hello world'))#my name is Bill,i am hello world
#您可以使用索引号 {0} 来确保参数被放在正确的占位符中:
M='my name {2} age {0} sex {1}'
print(M.format(18,'男','李四'))#my name 李四 age 18 sex 男