Python3 string Attribute (iv)

Source: Internet
Author: User


S.partition (Sep) (head, Sep, tail)

Search for the separator Sep in S, and return to the part before it,
The separator itself, and the part after it. If the separator is not
Found, return S and both empty strings.

The partition function, the parameter is the dividing character, divides into three parts, (parameter before, parameter, parameter); If the argument is not found, returns the original string and two empty strings. There are several parameters, whichever is the first one. S.partition (Sep)-(head, Sep, tail) takes the last parameter as a standard.

1>>>a2 'acbsdwf124'3>>> A.partition ('D')4('Acbs','D','wf124')5>>> A.partition ('I')6('acbsdwf124',"',"')7>>> A.partition ('SD')8('ACB','SD','wf124')9>>> a='Hello world Hello Huhu!'Ten>>> A.partition ('Hello') One("','Hello','World Hello Huhu!')


S.replace (old, new[, Count]), str

Return a copy of S with all occurrences of substring
Old replaced by new. If The optional argument count is
Given, only the first count occurrences is replaced.


1>>> a='1a2a3a4a5a'2>>> A.replace ('a','b')3 '1b2b3b4b5b4>>> A.replace ('a','b',3)5 '0b1b2b3a4a5a #替换前三个


S.split (Sep=none, maxsplit=-1), List of strings

Return A list of the words in S, using Sep as the
Delimiter string. If Maxsplit is given, at the most maxsplit
Splits is done. If Sep is no specified or is None and any
Whitespace string is a separator and empty strings are
Removed from the result.

S.rsplit (Sep=none, maxsplit=-1), List of strings the second parameter, which is divided from the right.

Text parsing, the default is a space, the space will be removed. Returns a list of strings.

1>>> a='Hello World, Huhu!'2>>>A.split ()3['Hello',' World',', Huhu','!']4>>> A.split (',')5['Hello World','Huhu!']6>>> a='Hello:world:huhu'7>>> A.split (':',2)8['Hello',' World','Huhu']9>>> A.split (':',1)Ten['Hello','World:huhu'] #从左侧划分.

S.splitlines ([keepends]), List of strings

Return a list of the lines in S, breaking on line boundaries.
Line breaks is not included in the resulting list unless keepends
is given and true

1>>> a="""Hello world!2 . .. second line3 ... splitlins test4..."""5>>>A.splitlines ()6['Hello world!','Second Line','splitlins Test']7>>>a.splitlines (True) #保留行分割符8['Hello world!\n','Second line\n','splitlins test\n']


S.swapcase () str

Return a copy of S with uppercase characters converted to lowercase
and vice versa.

Letter Case Conversion

1 >>> a='abcdefgh'2 >>> a.swapcase (3)  'abcdefgh'


S.zfill (width), str

Pad A numeric string S with zeros in the left, to fill a field
of the specified width. The string S is never truncated.

For a given length, add 0 to the left

1 >>> a2'abcdefgh'3 >>> A.zfill ( Ten )4'00ABCDefgh'

Python3 string Attribute (iv)

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