String Methods
split()
Split text into a list of pieces.
str.split(sep=None, maxsplit=-1)
str.split() splits the string into a list of substrings using sep as the delimiter.
If sep is omitted or None, any consecutive whitespace is treated as a delimiter and leading/trailing whitespace is discarded.
Arguments
Parameters
| Name | Description |
|---|---|
| sep | The separator to split on; if omitted, whitespace is used. |
| maxsplit | Maximum number of splits; -1 means no limit. |
Result
Returns
A list of pieces from the string.
Run it
Try it
Run it and change it
text = "red,green,blue"
print(text.split(",")) Output
Press Run to see what it prints.Worth knowing
- Whitespace splitting:
'a b c'.split()splits on arbitrary runs of spaces, tabs, and newlines into['a', 'b', 'c'].