Str= ' Python String function '
Generate string variable str= ' python string function '
string length get: Len (str)
Example: print '%s length=%d '% (Str,len (str))
One, letter handling
all uppercase: Str.upper ()
All lowercase: str.lower ()
Case Interchange: Str.swapcase ()
First letter 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 ())
Second, formatting related
get fixed length, right alignment, left not enough 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 ())
Third, 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 '))
Third, 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))
Four, the string to go to the space and to specify the 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 (")
Five, by default, separated by a space
Str= ' a b C de '
print '%s strip=%s '% (Str,str.split ())
Str= ' A-b-c-de '
print '%s strip=%s '% (str,str.split ('-'))
Vi. Correlation of string judgments
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 ())
Common character method string processing in Python