Methods for Python String segmentation

Source: Internet
Author: User

In peacetime work, found that the method of string segmentation with more, the following method to summarize the split string:
First type: Split () function
The split () function should be said to be the most used function of the split string
Str.split (' separator ')
After the split operation, a list is returned.

Note: Of course, if your string contains one or more spaces, you can simply Str.split ().

For example:

>>> a = "Hello,python,good Night" >>> a.split (', ') [' hello ', ' python ', ' Good Night ']

The second type: Splitlines () function

The Splitline () function is a string split by "line"


Object.splitlines ()

After the split operation, a list is returned.

For example:

>>> a = ' I have a pen I had           a apple            apple pen        ' >>> a.splitlines () [' I have a pen ', ' i hav e a Apple ', ' apple Pen ']

Note:A. If the above object A in addition to the newline character, before and after the string has spaces, you can use the strip () function to remove the space before and after the string

B. For the Splitlines () function There is a keepends bool parameter, when Keepends is true: the tail of each row in the split is \ n; when Keepends is false: \ n is not preserved at the end of each line;

The third type: Import re module for string multi-character segmentation

When we are dealing with certain strings, we need to split multiple characters in a string, but for the first method split () can use only one symbol for string splitting at a time, so we can use this method


Import RE Module first: Import re

After: Re.split (' delimiter 1| 2 ', objects)--different delimiters are spaced with ' | ' (the delimiter needs to be escaped with ' \ '), and then objects the string object to be split.

For example:

>>> e = "[email protected]" >>> import re>>> re.split (' @|\. ', e) [' 852317006 ', ' qq ', ' com ']

Note that the '. ' Above delimiter is "\. ' For the escape expression '. ' To be split.

The follow-up will also be supplemented .... ,,

Methods for Python String segmentation

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