Python string Methods

Source: Internet
Author: User

Type Method Annotation
Fill Center (width [, fillchar]), ljust (width [, fillchar]), fill ust (width [, fillchar]), zfill (width), expandtabs ([tabsize]) L

the fillchar parameter specifies the characters to be filled, the default value is space.

L

as the name suggests, zfill () is filled with 0 characters, commonly used when outputting values

L

the tabsize parameter of expandtabs () is 8 by default. Its function is to convert the tabs in a string into appropriate spaces.

Delete strip ([chars]), lstrip ([chars]), rstrip ([chars]) * The Strip () function family removes spaces at both ends of a string, A blank string. whitespace constant definition.
Deformation Lower (), upper (), capitalize (), swapcase (), title () The title () function is special. It is used to uppercase the first letter of each word and convert the non-first letter of the word to lowercase (EnglishArticle). >>> 'Hello world! '. Title ()' Hello world! 'Because the title () function does not remove the blank characters at both ends of the string and does not replace the consecutive blank characters with a space, we recommend that you use the capwords (s) function in the string module, it can remove the blank spaces at both ends, and then replace the consecutive blank spaces with one space. >>> 'HelloWorld! '. Title ()

'HelloWorld! '

>>> String. capwords ('HelloWorld! ')

'Hello world! '
Split Partition (SEP), rpartition (SEP), splitlines ([keepends]), split ([Sep [, maxsplit]), rsplit ([Sep [, maxsplit]) L

* Partition ()
The function family is a new method in version 2.5. It accepts a string parameter and returns a tuple object with three elements. If Sep does not appear in the parent string, the returned value is (SEP,
'',''); Otherwise, the first element of the returned value is the left side of the SEP, the second element is the SEP itself, and the third element is the right side of the SEP.

L

The maxsplit parameter is the number of times of splitting, that is, the maximum number of times of splitting. Therefore, the returned value is maxsplit + 1 element at most.

L

The return values of S. Split () and S. Split ('') are different.

>>> 'HelloWorld! '. Split ()

['Hello', 'World! '] >>>' HelloWorld! '. Split ('')

['','', 'Hello', '', 'World! '] Production
The reason for the difference is that when the SEP parameter is ignored or the SEP parameter is none, it is split ()
Use two differentAlgorithm. For the former, split ()
First, remove the blank characters at both ends of the string, and then use a blank string of any length as the delimiter to split the string (that is, a consecutive blank string is treated as a single blank string ); the latter considers two consecutive
There is an empty string between Sep. Therefore, for empty strings (or blank strings), their return values are also different: >>> ''. split () [] >>> ''. split ('') ['']
connection join (SEQ) high efficiency of the join () function (relative to the sum of loops ), make it one of the most noteworthy string methods. Its function is to concatenate a string that can be iterated into a long string, such as >>> conf = {'host': '100. 0.0.1 ',... 'db': 'spam',

... 'user': 'sa ',

... 'passwd': 'eggs'}

';'. join ("% s = % s" % (K, V) for K, V in Conf. iteritems () 'passswd = eggs; DB = spam; user = sa; host = 127.0.0.1 '
determination isalnum (), isalpha (), isdigit (), islower (), isupper (), isspace (), istitle (), startswith (prefix [, start [, end]), endswith (suffix [, start [, end]) all these functions are simple and well-known. Note that the * With () function family can accept the optional start and end parameters to optimize the performance. In addition, since py2.5, the prefix parameter of the * With () function family can accept real parameters of the tuple type. If one element in the real parameter can match, true is returned.
Search Count (sub [, start [, end]), find (sub [, start [, end]), index (sub [, start [, end]), rfind (sub [, start [, end]), rindex (sub [, start [, end]) If the find () function family cannot be found,-1 is returned. The index () function family throws a valueerror exception. You can also use the in and not in operators to determine whether a template exists in the string.
Replace Replace (old, new [, Count]), translate (Table [, deletechars]) L

Replace () the Count parameter of the function is used to specify the maximum number of replicas

L

the parameter table of translate () can be set by string. maketrans (FRM, To) generation

L

translate () does not fully support Unicode objects. We recommend that you do not use it.

encoding encode ([encoding [, errors]), decode ([encoding [, errors]) This
is a pair of reciprocal operations, used to encode and decode strings. Because STR is platform-related, the internal code used depends on the operating system environment, while Unicode is platform-independent and is the internal string storage method of python
. Unicode can be converted into STR by encoding (encode), while STR can also be converted into Unicode by decoding (decode.

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.