Python's built-in string handling functions are detailed (covering everyday use)

Source: Internet
Author: User
str= ' python String function '

Generate string variable str= ' python string function '

String length acquisition: Len (str)
Example: print '%s length=%d '% (Str,len (str))

Letter Handling
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 ())
Formatting related
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))

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 ())

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.