str= ' python String function '
Generate string variable str= ' python string function '
String length fetch: Len (str)
Example: print '%s length=%d '% (Str,len (str))
Letter Processing
All capitals: Str.upper ()
All lowercase: str.lower ()
Case Exchange: Str.swapcase ()
Capitalize first letter, remaining lowercase: str.capitalize ()
Initial capital: 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
get fixed length, right alignment, left not enough to be padded with spaces: str.ljust (width)
Get fixed length, left-aligned, right not enough to be padded with spaces: str.ljust (width)
Get fixed length, middle alignment, two sides not enough to be padded with blanks: str.ljust (width)
Get fixed length, right alignment, left insufficient with 0 padded
print '%s ljust=%s '% (Str,str.ljust (20))
print '%s rjust=%s '% (Str,str.rjust (20))
print '%s center=%s '% (Str,str.center (20))
print '%s zfill=%s '% (Str,str.zfill (20))
String Search Related
searches for the specified string and does not return -1:str.find (' t ')
Specify starting Position search: Str.find (' t ', start)
Specify start and End locations search: Str.find (' t ', start,end)
Start on the right: Str.rfind (' t ')
Number of specified strings searched: Str.count (' t ')
All of the above methods can be replaced by index, the difference is to use the index to find not to throw the exception, and found return-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 substitution related
replace old with New:str.replace (' old ', ' new ')
Replaces the specified number of old 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))
String to go to space and to specify characters
go to both sides space: Str.strip ()
Go to left Space: Str.lstrip ()
Go to right Space: Str.rstrip ()
Go to both sides of the string: Str.strip (' d '), and accordingly there are 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 the specified character as an array: Str.split (")
Separate by Space 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 ('-'))
The
string determines whether the relevant
begins with start: Str.startswith (' start ')
ends with end: Str.endswith (' ")
Whether all letters or numbers: Str.isalnum ()
Full letter: Str.isalpha ()
is 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 ())