How to Split a String in Python (Delimiters & Maxsplit)
Learn how to split a string into a list in Python using the split() method. Explore splitting by spaces, commas, newlines, and limiting the split count.
Explanation
Splitting strings is one of the most common text processing operations. Whether you are parsing a CSV file, breaking up logs, processing user input, or extracting words from a document, Python's built-in string methods make this task trivial. The key method for this is `.split()`, which breaks a string into a list of substrings.
By default, calling `split()` without any arguments splits the string by any whitespace (spaces, tabs, newlines) and discards consecutive whitespace. If you need to partition a string based on a specific character, such as a comma in CSV data, you pass that character as the separator (e.g., `text.split(",")`).
The `split()` method also accepts a second optional parameter called `maxsplit`. This determines the maximum number of splits that should be performed. The remainder of the string is returned as the final element of the list, which is highly useful when you only want to parse the first few tokens of a command or message.
Step-by-Step Implementation
- 1
Call the .split() method on the string variable.
- 2
Pass a separator string like ',' or '\n' to split by a custom delimiter.
- 3
Use the maxsplit argument to stop splitting after a specific number of items.
Code Example
This script demonstrates splitting strings using default delimiters, custom delimiters, and limiting splits using maxsplit.
sentence = "Python is fun to learn"
csv_data = "apple,banana,orange,grape"
# 1. Split by whitespace (default)
words = sentence.split()
print("Whitespace split:", words)
# 2. Split by comma separator
fruits = csv_data.split(",")
print("Comma split:", fruits)
# 3. Using maxsplit parameter
limited_split = csv_data.split(",", maxsplit=2)
print("Maxsplit of 2:", limited_split)Whitespace split: ['Python', 'is', 'fun', 'to', 'learn']
Comma split: ['apple', 'banana', 'orange', 'grape']
Maxsplit of 2: ['apple', 'banana', 'orange,grape']Frequently Asked Questions
How does split() handle consecutive spaces?
By default (without arguments), consecutive spaces are treated as a single separator. However, if you pass " ", consecutive spaces will produce empty strings in the output list.
How can I split a string by multiple different delimiters?
To split by multiple delimiters (like commas, spaces, and semicolons), use regular expressions via the re.split() function.
Related How-To Guides
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Variables & Data Types Explained
Understand Python variables and core data types (strings, integers, floats, booleans). A complete beginner guide to memory assignment in Python.
Python String Methods
A complete reference guide for Python string manipulation. Master formatting, searching, splitting, replacing, and checking string properties.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.