Simple use of python string method, python string method

Source: Internet
Author: User

Simple use of python string method, python string method

Learn how to use the python string method. Make a trial of each 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:

  

1 >>> s = 'wwwwww'2 >>> scap = s.capitalize()3 >>> scap 4 '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.

1 >>> scen0 2 'YYYYYYYwwwwwwYYYYYYY'3 >>> sends0 = scen0.endswith('Y',1,19)4 >>> sends05 True6 >>> sends1 = scen0.endswith('w',1,10)7 >>> sends18 True

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

1 >>> s2 'YYYYYYYwwwwwwYYYYYYY'3 >>> t = 'w'4 >>> sfind = s.find(t,0,19)5 >>> sfind6 77 >>> srfind = s.rfind(t,1,19)8 >>> srfind9 12

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

1 >>> s2 'YYYYYYYwwwwwwYYYYYYY'3 >>> sisa = s.isalnum()4 >>> sisa5 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.

1 >>> sdigit = 'www000'2 >>> sd = sdigit.isdigit()3 >>> sd4 False5 >>> sdigit0 = '1234567890'6 >>> sd0 = sdigit0.isdigit()7 >>> sd08 True

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

1 >>> seq = 'WWWWWWW'2 >>> s = 'y'3 >>> snew = s.join(seq)4 >>> snew5 'WyWyWyWyWyWyW'

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

1 >>> snew2 'WyWyWyWyWyWyW'3 >>> s =snew.lower()4 >>> s5 '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.

1 >>> s = 'WyWyWyWy'2 >>> ssw = s.swapcase()3 >>> ssw4 '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.

1 >>> s = 'www'2 >>> sz = s.zfill(10)3 >>> sz4 '0000000www'

 

Related Article

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.