Simple use of the python string method

Source: Internet
Author: User
The following is a simple introduction to the python string method. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the use of the python string method. we will try every method listed in the book and record the results to facilitate future queries.

(1) s. capitalize (); function: returns a copy of the string and upper-case the first letter. Use:

>>> s = 'wwwwww' >>> scap = s.capitalize() >>> scap  'Wwwwww'

(2) s. center (width, char); function: returns a string with the length of width in the middle of the s string. by default, other parts are filled with spaces. Otherwise, the char parameter is used. Example:

>>> s 'wwwwww'>>> s = 'wwwwww'>>> scen = s.center(20)>>> scen'    wwwwww    '>>> scen0 = s.center(20,'Y')>>> scen0'YYYYYYYwwwwwwYYYYYYY'>>>

(3) s. count (t, start, end); function: return the number of t in string s (or in the string's sub-segment ). Example:

>>> scen0'YYYYYYYwwwwwwYYYYYYY'>>> scoun = scen0.count('w',0,19)>>> scoun6

(4) s. endswith (x, start, end); function: if s (or split with start and end) ends with string x, True is returned; otherwise, False is returned.

>>> scen0 'YYYYYYYwwwwwwYYYYYYY'>>> sends0 = scen0.endswith('Y',1,19)>>> sends0True>>> sends1 = scen0.endswith('w',1,10)>>> sends1True

(5) s. find (t, start, end); function: returns the leftmost position of string t in s (or start: end slice). If t is not found,-1 is returned ,. Use s. rfind (t, start, end) to find the rightmost position of t. Example:

>>> s'YYYYYYYwwwwwwYYYYYYY'>>> t = 'w'>>> sfind = s.find(t,0,19)>>> sfind7>>> srfind = s.rfind(t,1,19)>>> srfind12

(6) s. isalnum (); function: if s is not empty, and each character isLetter/digitReturns True.

 >>> s 'YYYYYYYwwwwwwYYYYYYY' >>> sisa = s.isalnum() >>> sisa True

(7) s. isalpha (); function: if s is not empty, and each character isLetterReturns True.

>>> s'YYYYYYYwwwwwwYYYYYYY'>>> sisal = s.isalpha()>>> sisalTrue>>> s1'ssss0000'>>> sisal0 = s1.isalpha()>>> sisal0False

(8) s. isdigit (); function: if s is not empty and each character is an ASCII number, True is returned.

>>> sdigit = 'www000'>>> sd = sdigit.isdigit()>>> sdFalse>>> sdigit0 = '1234567890'>>> sd0 = sdigit0.isdigit()>>> sd0True

(9) s. islower (); function: if s has at least one lowercase character and all lowercase characters are lowercase, True is returned.

>>> s0 = '1234567890'>>> s0lower = s0.islower()>>> s0lowerFalse>>> s1 = '1234WWW'>>> s1lower = s1.islower()>>> s1lowerFalse>>> s2 = '123456789w'>>> s2lower = s2.islower()>>> s2lowerTrue

(10) s. isspace (); function: if s is not empty and every character in s is a blank character, True is returned.

>>> s = ''>>> s0 = s.isspace()>>> s0False>>> s = '  '>>> s0 = s.isspace()>>> s0True

(11) s. istitle (); function: Returns True if s is a non-empty string with uppercase letters.

>>> s = 'wy'>>> s0 = s.istitle()>>> s0False>>> s = 'Wy'>>> s0 = s.istitle()>>> s0True

(12) s. isupper (); function: Returns True if s has at least one character that can be written in uppercase and all characters that can be written in uppercase.

>>> s = 'wy'>>> s0 = s.isupper()>>> s0False>>> s = 'Wy'>>> s0 = s.isupper()>>> s0False>>> s = 'WY'>>> s0 = s.isupper()>>> s0True

(13) s. join (seq); function: return the result of sequence seq after joining, and s is between every two items.

>>> seq = 'WWWWWWW' >>> s = 'y' >>> snew = s.join(seq) >>> snew 'WyWyWyWyWyWyW'

(14) s. lower (); function: converts characters in s to lowercase letters.

>>> snew 'WyWyWyWyWyWyW' >>> s =snew.lower() >>> s 'wywywywywywyw'

(15) s. replace (t, u, n); function: returns a copy of s, where each t (or up to n) is replaced by u.

>>> s'wywywywywywyw'>>> sre = s.replace('w','W')>>> sre'WyWyWyWyWyWyW'>>> s'wywywywywywyw'>>> sre = s.replace('w','W',4)>>> sre'WyWyWyWywywyw'

(16) s. swapcase (); function: return a copy of s, and uppercase and lowercase.

>>> s = 'WyWyWyWy' >>> ssw = s.swapcase() >>> ssw 'wYwYwYwY'

(17) s. zfill (w); function: returns a copy of s. if it is shorter than w, add 0 at the beginning to set its length to w.

>>> s = 'www' >>> sz = s.zfill(10) >>> sz '0000000www'

The simple use of the python string method mentioned above is all the content that I have shared with you. I hope you can give me a reference and support me a lot.

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.