Sort the built-in String processing functions of Python, including Letter Processing, formatting, String SEARCH, String replacement, and so on. str = 'python String function'
Generate the String variable str = 'python String function'
String Length acquisition: len (str)
Example: print '% s length = % d' % (str, len (str ))
Letter Processing
All uppercase: str. upper ()
All lower case: str. lower ()
Case-insensitive: str. swapcase ()
Upper-case letters and lower-case letters: str. capitalize ()
Uppercase: str. title ()
Print '% s lower = % s' % (str, str. lower ())
Print '% s upper = % s' % (str, str. upper ())
Print '% s swapcase = % s' % (str, str. swapcase ())
Print '% s capitalize = % s' % (str, str. capitalize ())
Print '% s title = % s' % (str, str. title ())
Formatting
Get fixed length, right alignment, left not enough space fill: str. ljust (width)
Get fixed length, left alignment, right side not enough space fill: str. ljust (width)
Get a fixed length and align in the middle. The two sides are not filled with spaces: str. ljust (width)
Get fixed length, right alignment
Print '% s ljust = % s' % (str, str. ljust (20 ))
Print '% s must ust = % s' % (str, str. Must ust (20 ))
Print '% s center = % s' % (str, str. center (20 ))
Print '% s zfill = % s' % (str, str. zfill (20 ))
String search
Search for the specified string.-1: str. find ('T') is not returned ')
Search at the specified start position: str. find ('T', start)
Search by specified start and end locations: str. find ('T', start, end)
Search for str. rfind ('T') from the right ')
How many specified strings are searched: str. count ('T ')
All the methods above can be replaced by indexes. The difference is that if index is not found, an exception is thrown, while find returns-1.
Print '% s find nono = % d' % (str, str. find ('nono '))
Print '% s find t = % d' % (str, str. find ('T '))
Print '% s find t from % d = % d' % (str, 1, str. find ('T', 1 ))
Print '% s find t from % d to % d = % d' % (str, 1, 2, str. find ('T', 1, 2 ))
# Print '% s index nono' % (str, str. index ('nono', 1, 2 ))
Print '% s rfind t = % d' % (str, str. rfind ('T '))
Print '% s count t = % d' % (str, str. count ('T '))
String replacement
Replace old with new: str. replace ('Old', 'new ')
Replace the old value of a specified number of times with new: str. replace ('Old', 'new', maxReplaceTimes)
Print '% s replace t to * = % s' % (str, str. replace ('T ','*'))
Print '% s replace t to * = % s' % (str, str. replace ('T',' * ', 1 ))
Character string with spaces and specified characters
Remove space on both sides: str. strip ()
Remove left space: str. lstrip ()
Right space: str. rstrip ()
Remove the strings on both sides: str. strip ('D'). The corresponding strings include lstrip and rstrip.
Str = 'python String function'
Print '% s strip = % s' % (str, str. strip ())
Str = 'python String function'
Print '% s strip = % s' % (str, str. strip ('D '))
Splits string by specified character into an array: str. split ('')
Separated by spaces by default
Str = 'a B c de'
Print '% s strip = % s' % (str, str. split ())
Str = 'a-B-c-de'
Print '% s strip = % s' % (str, str. split ('-'))
String judgment
Start or not: str. startswith ('start ')
End with: str. endswith ('end ')
Whether all are letters or numbers: str. isalnum ()
Full letter: str. isalpha ()
Full number: str. isdigit ()
Lowercase?: str. islower ()
Uppercase or not: str. isupper ()
Str = 'python String function'
Print '% s startwith t = % s' % (str, str. startswith ('T '))
Print '% s endwith d = % s' % (str, str. endswith ('D '))
Print '% s isalnum = % s' % (str, str. isalnum ())
Str = 'pythonstringfunction'
Print '% s isalnum = % s' % (str, str. isalnum ())
Print '% s isalpha = % s' % (str, str. isalpha ())
Print '% s isupper = % s' % (str, str. isupper ())
Print '% s islower = % s' % (str, str. islower ())
Print '% s isdigit = % s' % (str, str. isdigit ())
Str = '000000'
Print '% s isdigit = % s' % (str, str. isdigit ())