Last modified: Nov 01, 2026
Python Split a String into a List
Splitting strings is a core Python skill. You often need to break text into words or fields. The split method handles this task. It returns a list of substrings. This guide covers syntax, separators, limits, and common errors.
You will see practical examples with outputs. You will also learn how to avoid common mistakes. By the end, you can parse text reliably in your own projects.
What Is str.split()?
The split method is available on every string. You call it on the string you want to split. It returns a list of strings. The default separator is any whitespace. That includes spaces, tabs, and newlines.
Use str.split() for basic cases. Consecutive whitespace is treated as one separator. Leading and trailing whitespace is ignored. This behavior is convenient for cleaning input.
text = "Python is easy to learn" words = text.split() # Split on whitespace print(words) # Show the resulting list ['Python', 'is', 'easy', 'to', 'learn'] The original string is not changed. A new list is created. You can reuse the original string for other operations. This makes split safe and predictable.
Splitting with a Specific Separator
Pass a string as the separator. The method splits at each occurrence. You get a list of parts. Use a comma for CSV-like data. Use a colon for key-value pairs.
data = "apple,banana,cherry" items = data.split(",") # Split at each comma print(items) ['apple', 'banana', 'cherry'] If the separator appears multiple times, you get empty strings. For example, splitting "a,,b" on a comma returns ['a', '', 'b']. Check your data for this case. You can filter empty strings later if needed.
Separators can be multiple characters. For example, split on "::" to parse custom formats. The method matches the exact sequence. It does not treat the characters separately.
Limiting Splits with maxsplit
The maxsplit argument limits the number of splits. The last element contains the rest of the string. This is useful when you only need the first few fields.
line = "name: Alice, age: 30, city: Paris" parts = line.split(",", 2) # Split at most twice print(parts) ['name: Alice', ' age: 30', ' city: Paris'] maxsplit controls how many splits happen, not the number of resulting items. With maxsplit=2, you get three items. Negative maxsplit means no limit. The default is -1, which splits everywhere.
Use maxsplit when parsing structured lines. It preserves the remainder as one piece. This avoids unnecessary splitting and keeps data intact.
Splitting from the Right with rsplit()
The rsplit() method works like split but starts from the right. It is useful for file paths or extensions. The maxsplit argument applies from the right side.
path = "/home/user/report.txt" parts = path.rsplit("/", 1) # Split at the last slash print(parts) ['/home/user', 'report.txt'] This approach is safer than splitting and taking the last element. It handles paths with multiple separators cleanly. You get the directory and filename separately.
rsplit is also helpful for parsing version strings or URLs. Split from the right when the important part is at the end. This keeps the left side intact.
Splitting Multiline Strings with splitlines()
The splitlines() method splits on line boundaries. It returns a list of lines without the newline characters. This is perfect for processing text files or logs.
text = "first line\nsecond line\nthird line" lines = text.splitlines() # Split into separate lines print(lines) ['first line', 'second line', 'third line'] You can keep the line endings by passing keepends=True. That is useful when you need to preserve original formatting. The default removes them for cleaner output.
splitlines handles different newline styles. It works with \n, \r\n, and \r. This makes it portable across operating systems. Use it instead of splitting on a single newline character.
Handling Whitespace and Empty Strings
Real data often has extra spaces. Use the strip() method to remove them. You can also filter empty strings after splitting. This keeps your list clean.
raw = " apple , banana , cherry " items = [item.strip() for item in raw.split(",")] items = [item for item in items if item] # Remove empty strings print(items) ['apple', 'banana', 'cherry'] Always validate input before splitting. If the string is empty, split returns an empty list. That is usually safe. But empty separators raise an error.
For whitespace-heavy input, split without arguments is best. It handles multiple spaces and tabs automatically. Then strip each item if you need clean tokens.
Common Mistakes and How to Fix Them
One common mistake is passing an empty separator. Python raises a ValueError. You cannot split on nothing. Use a space or another character instead.
text = "abc" # text.split("") # ValueError: empty separator Another mistake is calling split on a non-string. Lists and numbers do not have this method. Convert the value to a string first if needed. Check your data types before splitting.
Also watch for trailing separators. They produce empty strings at the end. Remove them with filtering or rstrip. This is common in CSV data with trailing commas.
Remember that split does not modify the original string. If you need the result, assign it to a variable. Forgetting this leads to confusing bugs.
Performance and Best Practices
The split method is fast and memory-efficient. It creates a new list and new strings. For very large inputs, consider processing line by line. Use splitlines for files to avoid loading everything at once.
Choose the simplest separator that works. Avoid regular expressions unless you need complex patterns. The re.split function is powerful but slower. Use it only when necessary.
Splitting is the opposite of joining. If you need to combine a list back into a string, read this guide on joining a list of strings in Python. It covers separators and edge cases.
When you print the split results, you may want to format them nicely. Compare Python string formatting methods to choose the best output style. For cleaner interpolation, see this complete guide to Python f-strings. These resources help you present data clearly.
Use maxsplit to avoid unnecessary work. If you only need the first field, split with maxsplit=1. This saves memory and time. It also makes your intent clear to readers.
Conclusion
Splitting a string into a list is a fundamental Python operation. Use split for simple whitespace or custom separators. Use rsplit when you need to split from the right. Use splitlines for multiline text. Remember to handle empty strings and strip whitespace. Avoid empty separators. With these tools, you can parse text reliably and efficiently.