Python's built in string handling function details the collation of overrides for everyday use

  • 2020-04-02 13:58:46
  • OfStack

String STR = "python function '

Generate String variable STR ='python String function'

String length: len(STR)
Example: print '%s length=%d' % (STR,len(STR))

Letters to deal with
All uppercase: STR. Upper ()
All lowercase: STR. Lower ()
Case swap: STR. Swapcase ()
Capitalize the first letter, lowercase: STR. Capitalize ()
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 ())
Format correlation
Get a fixed length, align it to the right, fill it with Spaces to the left: STR. Ljust (width)
Str.ljust (width) gets a fixed length, align it to the left, fill it up with Spaces to the right: STR. Ljust (width)
Str.ljust (width) : str.ljust(width)
Get a fixed length, align to the right, the left is not enough to fill with 0
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 correlation
Search for the specified string without returning -1: STR. Find ('t')
Specify start location search: STR. Find ('t',start)
Specify start and end location search: STR. Find ('t',start,end)
Find: STR. Rfind ('t')
How many specified strings are found: STR. Count ('t')
All of the above methods can be replaced by index, except that index does not 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 ', 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 correlation
Replace ('old','new')
Replace ('old','new',maxReplaceTimes)
Print '% s to replace t * = % s' % (STR, STR. Replace (' t', '*'))
Print '% s to replace t * = % s' % (STR, STR. Replace (' t', '*', 1))

String to Spaces and to specified characters
STR. Strip ()
Left space: STR. Lstrip ()
To the right: STR. Rstrip ()
Str.strip ('d'), lstrip, rstrip
STR =' python String function '
Print '%s strip=%s' % (STR,str.strip())
String STR = "python function '
Print '%s strip=%s' % (STR, STR. Strip ('d'))

Split string as array by specified character: STR. Split (')

The default is 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
Start: STR. Startswith ('start')
End: STR. Endswith ('end')
Is it all letters or Numbers: STR. Isalnum ()
Full letter: STR. Isalpha ()
Full digit: STR. Isdigit ()
All lowercase: STR. Islower ()
STR. Isupper ()
String STR = "python 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 ())


Related articles: