1. Basic syntax
split() is a string in Python (str
) object is a very powerful built-in method for splitting strings into substrings by specified delimiters and returning a containing themList。
(sep = None, maxsplit = -1)
text = "a-b-c-d-e-f" parts = ('-', 3) print(parts) # Output: ['a', 'b', 'c', 'd-e-f']
1. sep (optional)
Specifies the separator used to split the string. If not specified, any whitespace characters (such as spaces, tab characters) are used by default.\t
, line breaks, etc.) as separators
2. maxsplit (optional)
Specifies the maximum number of splits. If this parameter is provided, the string will be split intomaxsplit + 1
Substring. The default value is-1
, means that there is no limit on the number of segmentation times, that is, as many segmentation as possible.
2. Common usage examples
1. Use the default delimiter (blank)
When not specifiedsep
When parameters,split()
Any whitespace characters will be used as delimiters and extra spaces will be automatically ignored.
text = "Hello World! How are you?" words = () print(words) # ['Hello', 'World!', 'How', 'are', 'you?']
2. Specify the delimiter
text = "apple,banana,cherry,dates" fruits = (',') print(fruits) # ['apple', 'banana', 'cherry', 'dates']
3. Use multiple characters as delimiters (used in conjunction with regular use)
import re text = "one--two---three----four" parts = ('--+', text) print(parts) # ['one', 'two', 'three', 'four']
4. Specify the maximum number of splits
text = "one two three four five" parts = (' ', 2) print(parts) # ['one', 'two', 'three four five']
5. Split empty strings
# Split empty string without specifying separatorsprint("".split()) # Output: [] # Split empty strings and specify delimiterprint("".split(',')) # Output: [''] # Split strings with only one characterprint("a".split(',')) # Output: ['a']
6. Handle continuous separators
text = "one,,two,three,,four" parts = (',') print(parts) # ['one', '', 'two', 'three', '', 'four']
If you need to ignore contiguous separators, you can use regular expressions:
import re text = "one,,two,three,,four" parts = (r',+', text) print(parts) # ['one', 'two', 'three', 'four']
3. Advanced usage
1. rsplit()
rsplit()
Methods andsplit()
Similar, but starts splitting from the right side of the string.
text = "one two three four" # Split from the leftprint((' ', 2)) # Output: ['one', 'two', 'three four'] # Split from the rightprint((' ', 2)) # Output: ['one two', 'three', 'four']
2. Split strings containing multiple delimiters
Using regular expression modulere
It can realize complex separator segmentation.
import re text = "apple;banana, cherry|dates" parts = (r'[;,|\s]+', text) print(parts) # ['apple', 'banana', 'cherry', 'dates']
4. Split multi-line strings
splitlines()
Methods are used to split multi-line strings according to line boundaries, andsplit('')
Similar functions can be implemented, butsplitlines()
More comprehensive, able to handle line breaks on different platforms.
text = "Hello\nWorld\r Python\rAnother line" # Use split('') print((' ')) # Output: ['Hello', 'World\r', 'Python\rAnother line'] # Use splitlines()print(()) # Output: ['Hello', 'World', 'Python', 'Another line']
5. The delimiter starts or ends of the string
If the delimiter appears at the beginning or end of the string,split()
An empty string will be included in the result.
text = ",apple,banana,,cherry," parts = (',') print(parts) # Output: ['', 'apple', 'banana', '', 'cherry', '']
If you do not need an empty string at the beginning and end, you can combine itstrip()
Methods to use:
parts = (',').split(',') print(parts) # Output: ['apple', 'banana', '', 'cherry']
6. Process Unicode characters
split()
The method also applies to strings containing Unicode characters.
text = "Hello, world, Python" parts = (',') print(parts) # Output: ['Hello', 'world', 'Python']
Summarize
This is the end of this article about the common usage of split() method in Python. For more related content on the usage of split() method, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!