str= ' python String function '
Generate string variable str= ' python string function '
String length acquisition: Len (str)
Example: print '%s length=%d '% (Str,len (str))
First, the letter processing
All caps: Str.upper ()
All lowercase: str.lower ()
Case swap: Str.swapcase ()
Initial capitalization, 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 ())
second, the format of the relevant
Get fixed length, right alignment, left not enough space to be padded: str.ljust (width)
Get fixed length, left alignment, right not enough space to complement: str.ljust (width)
Get fixed length, middle alignment, not enough space on both sides: str.ljust (width)
Get fixed-length, right-aligned, left less than 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))
Three, string search related
search specified string, no return -1:str.find (' t ')
Specify starting position search: Str.find (' t ', start)
Specify start and end search: Str.find (' t ' , start,end)
search from the right: Str.rfind (' t ')
How many specified strings have been searched: Str.count (' t ')
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 ', up))
# Print '%s index Nono '% (Str,str.index (' Nono ', $))
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 ())
There are other common Python string handler functions that are not updated regularly.
Common processing methods for string (str) in Python