Python built-in string processing function collation

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 ')
specified starting position search: str.find (' T ', start)
Specify start and end position search: str.find (' T ', start,end)
Find from the right: str.rfind (' T ')
How many specified strings were searched: str.count (' T ')
All of the above methods can be replaced with index, the difference is that using index lookup 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 '))
#print '%s index Nono '% (str,str.index (' nono ',)
Print '%s rfind t=%d '% (str,st R.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 '))

split string by specified character array: str.split (')

Default by 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 ('-'))

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


This article is from the "baby god" blog, make sure to keep this source http://babyshen.blog.51cto.com/8405584/1912749

Python built-in string processing function collation

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.