s = "abcaDa a" s2 = "123a abc ABCSAa s " s3 = "\tas \t\tb123" s4 = ' &abc123 c ## '
1.str.capitalize()
print("s.capitalize() = {function}".format(function = s.capitalize()))
Output:
s.capitalize() = Abcada a
2.str.upper()
print("s.upper() = {function}".format(function = s.upper()))
Output:
s.upper() = ABCADA A
3.str.lower()
print("s.lower() = {function}".format(function = s.lower()))
Output:
s.lower() = abcada a
4.str.swapcase()
print("s.swapcase() = {function}".format(function = s.swapcase()))
Output:
s.swapcase() = ABCAdA A
5.str.title()
print("s2.title() = {function}".format(function = s2.title()))
Output:
s2.title() = 123A Abc Abcsaa S
6.str.center()
print("s2.center() = {function}".format(function = s2.center(19,'&'))) print("s2.center() = {function}".format(function = s2.center(20,'&')))
Output:
#s2 = 123a abc ABCSAa s s2.center() = &123a abc ABCSAa s s2.center() = &123a abc ABCSAa s &
7.str.expandtabs()
print("s3.expandtabs ={function}".format(function = s3.expandtabs())) print("s3.expandtabs ={function}".format(function = s3.expandtabs(0))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(5))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(8))) print("s3.expandtabs ={function}".format(function = s3.expandtabs(9)))
Output:
#s3 = "\tas \t\tb123" s3.expandtabs = as b123 s3.expandtabs =as b123 s3.expandtabs = as b123 s3.expandtabs = as b123 s3.expandtabs = as b123
8.String_len = len(str)
print("s2_length = {0}".format(len(s2)))
Output:
s2_length = 18
9.str.startswith()
print("s.startswith() = {function}".format(function = s.startswith('ab'))) print("s.startswith() = {function}".format(function = s.startswith('D',2))) print("s.startswith() = {function}".format(function = s.startswith('c',2,5))) print("s.startswith() = {function}".format(function = s.startswith('c',2,100)))
Output:
#s = abcaDa a s.startswith() = True s.startswith() = False s.startswith() = True s.startswith() = True
10.endswith()
print("s.endswith() = {function}".format(function = s.endswith('Da a'))) print("s.endswith() = {function}".format(function = s.endswith('a',-1))) print("s.endswith() = {function}".format(function = s.endswith('Da a',-4))) print("s.endswith() = {function}".format(function = s.endswith('c',-8,-5))) print("s.endswith() = {function}".format(function = s.endswith('c',-7,-5))) print("s.endswith() = {function}".format(function = s.endswith('c',-7,-4))) print("s.endswith() = {function}".format(function = s.endswith('Da a',-1,6)))
Output:
#s = abcaDa a s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = True s.endswith() = False s.endswith() = False
11.s.find()
print("s.find() = {function}".format(function = s.find('a'))) print("s.find() = {function}".format(function = s.find('ab'))) print("s.find() = {function}".format(function = s.find('f'))) print("s.find() = {function}".format(function = s.find('b',-8,-5))) print("s.find() = {function}".format(function = s.find('b',0)))
Output:
s.find() = 0 s.find() = 0 s.find() = -1 s.find() = 1 s.find() = 1
12.s.index()
print("s.index() = {function}".format(function = s.index('a'))) print("s.index() = {function}".format(function = s.index('ab'))) print("s.index() = {function}".format(function = s.index('D',-8,5))) print("s.index() = {function}".format(function = s.index('b',0)))
Output:
#s = abcaDa a s.index() = 0 s.index() = 0 s.index() = 4 s.index() = 1
13.str.strip()
print("s3.strip() = {function}".format(function = s3.strip()))
Output:
#s3 = as b123 s3.strip() = as b123
14.str.rstrip()
print("s4.rstrip() = {function}".format(function = s4.rstrip())) print("s4.rstrip() = {function}".format(function = s4.rstrip('# c')))
Output:
#s4 = ' &abc123 c ## ' s4.rstrip() = &abc123 c ## s4.rstrip() = &abc123
15.str.lstrip()
print("s4.lstrip() = {function}".format(function = s4.lstrip())) print("s4.lstrip() = {function}".format(function = s4.lstrip(' &')))
Output:
#s4 = ' &abc123 c ## ' s4.lstrip() = &abc123 c ## s4.lstrip() = abc123 c ##
16.str.count()
print("s.count() = {function}".format(function = s.count('a'))) print("s.count() = {function}".format(function = s.count('Fa'))) print("s.count() = {function}".format(function = s.count('a',-7)))
Output:
#s = "abcaDa a" s.count() = 4 s.count() = 0 s.count() = 3
17.str.split()
print("s2.split() = {function}".format(function = s2.split())) print("s2.split() = {function}".format(function = s2.split(' ',0))) print("s2.split() = {function}".format(function = s2.split('a',1))) print("s2.split() = {function}".format(function = s2.split('a',-1)))
Output:
#s2 = "123a abc ABCSAa s " s2.split() = ['123a', 'abc', 'ABCSAa', 's'] s2.split() = ['123a abc ABCSAa s '] s2.split() = ['123', ' abc ABCSAa s '] s2.split() = ['123', ' ', 'bc ABCSA', ' s ']
18.str.format()
print("s = {}|s2 = {}|s3 = {}".format(s,s2,s3)) print("s = {0}|s2 = {1}|s3 = {2}|s = {0}|s3 = {2}".format(s,s2,s3)) print("s = {s}{sep}s2 = {s2}{sep}s3 = {s3}".format(s = s,s2 = s2,s3 = s3,sep = '|'))
Output:
s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123 s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123|s = abcaDa a|s3 = as b123 s = abcaDa a|s2 = 123a abc ABCSAa s |s3 = as b123
19.str.replace()
print("s.replace() = {function}".format(function = s.replace('ac','A'))) print("s.replace() = {function}".format(function = s.replace('a','A'))) print("s.replace() = {function}".format(function = s.replace('a','A',2)))
Output:
#s = "abcaDa a" s.replace() = abcaDa a s.replace() = AbcADA A s.replace() = AbcADa a
20.is函数
str.isalnum() #判断是否由数字或字母组成
str.isdecimal() #判断是否含有十进制数字
str.isdigit() #判断是否含有数字
str.islower() #判断是否含有小写字母
str.isupper() #判断是否含有大写字母
str.isnumeric() #判断是否只包含数字字符
str.isspace() #判断是否只含有空格
str.istitle() #判断是否经过title()函数处理过后的标题