#一些重要的字符串内置方法:
print(st.count("l")) #统计元素个数
print(st.center(50,"-")) #居中
print(st.startswith("he")) #判断以某个内容开头
print(st.find("t")) #查找到第一个元素,并将索引值返回 空格也算
print(st.format(name="alex",age=37)) #格式化输出的另一种方式st="hello kitty {name} is {age}"
print("My Title".lower()) #所有大写变小写
print("My Title".upper()) #所有小写变大写
print("\tMy Title\n".strip()) #把开头和结尾的空格去掉以及换行符和制表符去掉
print("My title title".replace("title","lesson")) #把两个title全部替换
print("My title title".replace("title","lesson",1)) #指定替换一次,只把前一个title替换掉,后一个保留
print("My title title".split("i",1)) #以i为分割符,从左边起分割一次 结果['My t', 'tle title']
st="hello kitty {name} is {age}"
print(st.count("l")) #统计元素个数
print(st.capitalize()) #首字母大写
print(st.center(50,"-")) #居中
print(st.endswith("y")) #以某个内容结尾
print(st.startswith("he")) #判断以某个内容开头
print(st.expandtabs(tabsize=10)) #st="hello \t kitty"
print(st.find("t")) #查找到第一个元素,并将索引值返回 空格也算
print(st.format(name="alex",age=37)) #格式化输出的另一种方式st="hello kitty {name} is {age}"
print(st.format_map({"name":"alex","age":22})) #用字典输出st="hello kitty {name} is {age}"
print(st.index("t"))
print("abc".isalnum()) #是否为字母或数字
print("1234".isdecimal()) #十进制数
print("124775".isdigit()) #判断是否为数字 不能为小数
print("243532.uuu".isnumeric()) #判断是否为数字 不能为小数
print("24add".isidentifier()) #判断非法字符 不能以数字开头
print("abc".islower()) #判断是否全部为小写
print("ABC".isupper()) #判断是否全部为大写
print(" a".isspace()) #判断是否全部为空格
print("My Title".istitle()) #判断首字母是否全为大写
print("My Title".lower()) #所有大写变小写
print("My Title".upper()) #所有小写变大写
print("My Title".swapcase()) #所有小写变大写,大写变小写
print("My Title".ljust(50,"*")) #包含My title总共50个字符,右边全为*
print("My Title".rjust(50,"*")) #包含My title总共50个字符,左边全为*
print("\tMy Title\n".strip()) #把开头和结尾的空格去掉以及换行符和制表符去掉
print(" My Title".lstrip()) #把左边的空格去掉以及换行符和制表符去掉
print(" My Title".rstrip()) #把右边的空格去掉以及换行符和制表符去掉
print("My title".replace("title","lesson")) #把括号里左边的替换为右边的
print("My title".replace("itle","lesson")) #把括号里左边的替换为右边的,也可以替换一部分
print("My title title".replace("title","lesson")) #把两个title全部替换
print("My title title".replace("title","lesson",1)) #指定替换一次,只把前一个title替换掉,后一个保留
print("My title title".rfind("y")) #从左往右寻找y的位置,为1
print("My title title".split(" ")) #用空格把字符串转变为列表
print("My title title".split("i")) #用i把字符串转变为列表,以i为分割符 结果为 ['My t', 'tle t', 'tle']
print("My title title".rsplit("i",1)) #以i为分割符,从右边起分割一次 结果['My title t', 'tle']
print("My title title".split("i",1)) #以i为分割符,从左边起分割一次 结果['My t', 'tle title']
print("My title title".title()) #标题,使每个字符首字母大写 结果My Title Title
字符串的内置方法
猜你喜欢
转载自blog.csdn.net/weixin_42999586/article/details/82263551
今日推荐
周排行