🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeHow to Split a String in Python

To turn a sentence into separate words, you can split its text at whitespace. Python’s str.split() returns the pieces as a list of strings and can also use a separator you specify.
I’ll show how to choose a separator and limit how many splits Python makes.
TL;DR: Split a String Into a Python List
Call str.split() without an argument to divide text at runs of whitespace, or pass a literal separator to divide at that exact text.
- Use maxsplit to limit how many separators are consumed.
- Choose an explicit separator when empty fields must remain visible.
- Use re.split() when any of several delimiters should match.
What Is str.split() in Python?
str.split() is a method that returns a list of strings cut from the original string. Its optional sep argument specifies a literal delimiter.
text.split(sep=None, maxsplit=-1)
With sep omitted or set to None, Python groups consecutive whitespace and ignores whitespace at either edge, while leaving the original string untouched.
How to Split a String Step by Step
Start by choosing the boundary present in your input.
Step 1: Split on whitespace
Call split() with no arguments to turn a sentence into words despite repeated spaces, tabs, or line breaks. I ran the examples below from a Python file. The first output contains three items, despite the tabs and repeated spaces in the source.
text = " red\tblue green "
print(text.split())
print("a,,b,".split(","))
print("name:message:tail".split(":", maxsplit=1))
print("a,,b,".split(",") == ["a", "", "b", ""])
The exact output is:
['red', 'blue', 'green'] ['a', '', 'b', ''] ['name', 'message:tail'] True

The final True verifies both empty fields remain in the result.
Step 2: Pass the delimiter you mean
Use a longer delimiter when the complete token marks the boundary. For example, “red||blue||green”.split(“||”) uses the two-character string || as one delimiter. To remove spaces after commas, split on comma-space or trim each field.
For line boundaries, text.splitlines() communicates the goal directly and recognizes newline and carriage-return/newline endings without keeping them in returned items.
split() returns strings, not converted numbers. Convert each item after splitting when the next operation needs numeric values, and handle conversion errors if the input is not controlled.
Step 3: Keep the remainder together
Set maxsplit when the first fields have structure but the final field may contain the delimiter too. For an explicit separator, zero means no cuts and the default -1 permits all matches. With whitespace mode, maxsplit=0 skips leading whitespace and returns the unsplit remainder.
Use rsplit() when the rightmost boundary matters, such as separating a filename extension.
Edge Cases and Other Delimiter Choices
Separator choice controls what empty input and repeated delimiters mean. I checked the empty-separator case and compared a literal split with a regex that accepts either comma or semicolon.
| Input case | Behavior | What to use |
|---|---|---|
| Empty text with no sep | Returns an empty list | split() |
| Empty text with a delimiter | Returns one empty field | split(“,”) |
| Repeated explicit delimiter | Creates empty fields between matches | Check fields before consuming them |
| Empty separator | Raises ValueError: empty separator | Use list(text) for individual characters |
If the delimiter is absent, non-empty input is returned as one item.
The empty-separator error is literal: ‘abc’.split(”) raises ValueError: empty separator. list(text) returns one string per Unicode code point, not necessarily one per user-perceived character.
I compared str.split() with re.split() on text containing two delimiter forms. The literal split leaves the other punctuation in a field, while the regex treats both marks as boundaries. Escape arbitrary input when building a regex.
Python’s csv.reader handles quoted fields. For related string checks, see the AskPython guides to the Python islower() method and Python isupper() method.
Conclusion
Use the standard-library references below when the input format carries rules beyond simple separators.
FAQ
These common questions distinguish a literal separator from several alternatives or individual characters.
How do I split a string by a character in Python?
Pass that character as the separator, such as text.split(“,”). Repeated separators can create empty list items.
How do I split a string into individual characters?
Use list(text). Calling text.split(“”) raises ValueError because the separator cannot be empty.
How can I split on multiple delimiters?
Use re.split() with a regular expression that matches the permitted delimiters. str.split() accepts one literal separator at a time.


