原生格式
>>> name = "abcdef"
>>> name = "abc\tdef\n" #加入制表符和换行符
>>>
>>> name
'abc\tdef\n'
>>> print(name) #效果
abc def
>>> name = r"abc\tdef\n" #r输出原生格式
>>> print(name)
abc\tdef\n
首字母大写
>>> name.capitalize()
'Abc\\tdef\\n'
大写变小写
>>> "ABcde".casefold()
'abcde'
指定字符长度及填充
>>> name.center(50,'-') #50长度,‘-1‘填充
'--------------------abc\\tdef\\n--------------------'
切片中取出现字符次数
>>> name.count("a",0,-1) #a取字符,从0开始,到-1结束
1
判断以什么结尾
>>> name
'abc\\tdef\\n'
>>> name.endswith("n")
True
找字符
>>> name = "abdcdessf"
>>> name.find("f")
8
>>> name.find("f",2) #从2开始找
8
>>> name.find("g",2) #找不到返回-1
-1
占位符
>>> a = "welcome {0} to login in,dear {1} user :)"
>>> a.format("john",3)
'welcome john to login in,dear 3 user :)'
>>> a = "welcome {name} to login in,dear {number} user :)"
>>> a.format(name = "jack", number = 5)
'welcome jack to login in,dear 5 user :)'
索引
>>> a
'welcome {name} to login in,dear {number} user :)'
>>> a.index(")")
47
整数
>>> '5'.isdigit()
True
小写
>>> "AAess".lower()
'aaess'
空格
>>> ''.isspace()
False
>>> ' '.isspace()
True
大写
>>> "AAAef".isupper()
False
>>> "AAAEF".isupper()
True
>>> "AAAe".upper()
'AAAE'
拼接
>>> name = ["aa","bb","cc"]
>>>
>>>
>>> "".join(name)
'aabbcc'
>>> " ".join(name)
'aa bb cc'
>>> "-".join(name)
'aa-bb-cc'
保留左边填充
>>> name.ljust(50,'*') #50个字符,以*填充
'aa-bb-cc******************************************'
保留右边填充
>>> name.rjust(50,'*')
'******************************************aa-bb-cc'
去掉两边的空格和换行符
>>> a = ' shjh '
>>> a
' shjh '
>>> a = '\t shjh \n'
>>> a
'\t shjh \n'
>>> print(a)
shjh
>>> a.strip()
'shjh'
>>> a = '\t sh\tjh \n'
>>>
>>> print(a)
sh jh
>>> a.strip()
'sh\tjh'
去掉左边的
>>> a = '\t sh\tjh \n'
>>> a.lstrip()
'sh\tjh \n'
去掉右边的
>>> a.rstrip()
'\t sh\tjh'
替换
>>> a = "today is rainny, not good"
>>> a.replace("rainny","sunny")
'today is sunny, not good'
列表—字符串
>>> a = ["1","2","3"]
>>> ",".join(a)
'1,2,3'
>>> a1=",".join(a)
>>> a1
'1,2,3'
字符串变列表
>>> a1
'1,2,3'
>>> a1.split()
['1,2,3']
>>> a1.split(",")
['1', '2', '3']
以空格分割
'1,2,3 456'
>>> a1.split(" ")
['1,2,3', '', '456']
大小写互换
>>> a="AAAAeeee"
>>> a.swapcase()
'aaaaEEEE'
长度以0补
'aaaaEEEE'
>>> a.zfill(30)
'0000000000000000000000AAAAeeee'
0 Comments