Common operations of strings include but are not limited to the following:
String replacement, deletion, interception, copy, connection, comparison, search, segmentation, etc
1 #capitalize: String initial uppercase: capitalize-Capitalize the first letter of the string and lowercase other letters 2 name = 'swhthaitun' 3 name.capitalize() 4 print(name.capitalize())
1 #casefold: String all letters lowercase: casefold-Converts all uppercase characters in a string to lowercase characters 2 name = 'HelloWorld' 3 name.casefold() #Convert characters in all languages to lowercase 4 name.lower() #lower Only right ASCII Valid, invalid for other languages 5 print(name.casefold()) 6 print(name.lower())
1 #center: String width fill, using the original string+Padded characters make up a new string of a specified length 2 name = 'swhthaitun' 3 #Fill the original string with the same number of characters 4 print(name.center(15)) 5 print(name.center(16,'*')) 6 name2 = 'HelloWorld' 7 print(name.center(20,'*'))
1 #count: Count the number of times a character appears in the string, or complete the above operations within the specified interval of the string 2 name = 'swhthaitun' 3 result = name.count('h') 4 print(result) #2 5 result2 = name.count('h',0,3) 6 print(result2) #1 7 name2 = 'HelloWorld' 8 result3 = name2.count('W') #Can not be replaced'w',Python Case sensitive 9 print(result3) #1
1 #encode: Encoding strings 2 name = 'swhthaitun' 3 name.encode() 4 print(name.encode()) #b'swhthaitun'
1 #endwith: Determines whether a string ends with a character or string, and the return value is a Boolean value 2 name = 'swhthaitun' 3 result1 = name.endswith('s') 4 print(result1) #False 5 result2 = name.endswith('n') 6 print(result2) #True 7 result3 = name.endswith('tun') 8 print(result3) #True
1 #expandtabs: Tab tabs'\t'Convert to tab Key split, default tabsize=8 2 li = 'sw\tht' 3 li.expandtabs(4) 4 print(li.expandtabs(4)) #sw ht 5 print(li.expandtabs()) #sw ht
1 #find: Find the specified string in the string, no event returned-1 2 name = 'swht' 3 name.find('s') 4 name.find('h') 5 name.find('a') 6 print(name.find('s'),name.find('h'),name.find('a')) #0 2 -1
1 #format: Format output string 2 li = "I'm {},{}" #among{}Placeholder 3 result = li.format('swht','Welcome to China') 4 print(result) #I'm swht,Welcome to China
1 #__contains__: Determine whether a character is included in the string, and the return value is Boolean 2 name = 'swhtkkskjj' 3 result = name.__contains__('swht') 4 print(result) #True
1 #index: Find the specified string in the string, and report an error if it cannot be found 2 name = 'swhthaitun' 3 result1 = name.index('h') 4 #result2 = name.index('m') 5 print(result1) #2 6 #print(result2) #There is no 'm' in the string. An exception is reported
1 #join: String splicing 2 name = 'swhthaitun' 3 '*'.join(name) 4 name.join('ab') 5 print(name.join('ab')) #aswhthaitunb 6 print('*'.join(name)) #s*w*h*t*h*a*i*t*u*n
1 #isalnum: Check to see if the string contains letters or numbers 2 name1 = 'swhthaitun' 3 name2 = '12345' 4 result1 = name1.isalnum() 5 result2 = name2.isalnum() 6 print(result1) #True 7 print(result2) #True
1 #isalpha: Check whether the string is completely composed of letters 2 name = 'swhthaitun' 3 result = name.isalpha() 4 print(result) #True
1 #isdecimal: Check if the string contains only decimal characters 2 name = '123456' 3 result = name.isdecimal() 4 print(result) #True1 # isdecimal() 2 # True: Unicode Number,, full angle number (double byte) 3 # False: Roman numeral, Chinese numeral 4 # Error: byte Number (single byte)