Python built-in string processing function collation
Font: [increase decrease] type: reprint time: 2013-01-29 I want to comment
Python's built-in string processing function collects the use of various string processing functions that are commonly used in Python
str= ' python String function '
Generate string variable str= ' python string function '
string length get: len (str)
example: print '%s length=%d '% (str,len (str))
Letter Handling
all caps: str.upper ()
All lowercase: str.lower ()
case-sensitive: str.swapcase ()
First uppercase, Remaining lowercase: str.capitalize ()
First 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 related
gets fixed length, right alignment, left insufficient space to complement: Str.ljust (width)
Get fixed length, left alignment, right not enough space to do: str.ljust (width)
Get fixed length, middle alignment, both sides are not enough space to complement: str.ljust (width)
Get fixed length, right alignment, left insufficient 0 completion
Print '%s ljust=%s '% (str,str.ljust ())
Print '%s rjust=%s '% (str,str.rjust)
Print '%s center=%s '% (str,str. Center ()
Print '%s zfill=%s '% (str,str.zfill ())
String Search Related
Search for specified string, no return -1:str.find (' t ')
Specify starting position search: str.find (' T ', start)
Specify start and end position search: str.find (' T ', start,end)
Search from right: str.rfind (' t ')
Number of specified strings searched: str.count (' t ')
All of the above methods can be replaced with index, the difference is to use the index to find not to throw an exception, and 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 ', $))
#print '%s Index Nono '% (str,str.index (' nono ', up))
Print '%s rfind t=%d '% (str,str.rfind (' t '))
Print '%s count t=%d '% (str,str.count (' t '))
String substitution related
Replace old to New:str.replace (' old ', ' new ')
Replace the specified number of times with old as 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))
String go to space and go to specify character
Go on both sides space: Str.strip ()
Left Space: Str.lstrip ()
Go right space: Str.rstrip ()
Go to both sides of the string: str.strip (' d '), corresponding to the Lstrip,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 a string by a specified character array: str.split (")
By default, separated by spaces
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 Correlation
Whether to start with start: str.startswith (' start ')
End With end: str.endswith (' end ')
Whether it is all letters or numbers: Str.isalnum ()
Full Letter: Str.isalpha ()
Whether full number: str.isdigit ()
Full Lowercase: str.islower ()
All Caps: 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= ' 3423 '
Print '%s isdigit=%s '% (str,str.isdigit ())
Python built-in string processing function collation