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
Usage:
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"
Usage:
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
Usage:
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