Sort out the built-in String processing functions of Python and collect the usage of various built-in String processing functions of common Python. 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 ())
Other common Python string processing functions are updated occasionally.