In Python, the split() method is one of the most commonly used string operations. It allows you to break a string into a list of
substrings based on a specified delimiter. If no delimiter is provided, the method defaults to splitting on whitespace, treating
consecutive spaces as a single separator. This makes the Python String split() Method especially useful for parsing text data.
split() method syntax
str.split(separator, maxsplit)
Both of the parameters are optional.
separator- The delimiter on which the string will be split. If not specified, whitespace is used by default.
maxsplit- Defines the maximum number of splits. If omitted or set to -1, there is no limit.
Python also provides rsplit() method, which works like split() but performs splits starting from the right when maxsplit is
specified.
Python split() method examples
1. Using the split method with default parameters (not passing any parameter explicitly).
s = "This is a test String"
#break String on spaces
list = s.split()
print(list)
Output
['This', 'is', 'a', 'test', 'String']
Since no parameter is passed with split() method so whitespace is used as separator. Note that consecutive whitespaces are regarded as a single separator when default is used.
2. Splitting on custom delimiters like comma (,) or pipe symbol (|).
s = "Chicago,Los Angeles,Seattle,Austin"
#break String on ,
list = s.split(',')
print(list)
s = "Chicago|Los Angeles|Seattle|Austin"
#break String on |
list = s.split('|')
print(list)
Output
['Chicago', 'Los Angeles', 'Seattle', 'Austin']
['Chicago', 'Los Angeles', 'Seattle', 'Austin']
3. Split string on backslash (\) symbol. With backslash it is better to use escape sequence (\\).
s = "c:\\users\\netjs\\python"
#break String on ,
list = s.split('\\')
print(list)
Output
['c:', 'users', 'netjs', 'python']
4. Limiting the splits using maxsplit parameter. Here split is done for max 2 items.
s = "Chicago|Los Angeles|Seattle|Austin"
#break String on |
list = s.split('|', 2)
print(list)
Output
['Chicago', 'Los Angeles', 'Seattle|Austin']
5. Using rsplit() method.
s = "Chicago|Los Angeles|Seattle|Austin"
#break String on |
list = s.rsplit('|', 2)
print(list)
Output
['Chicago|Los Angeles', 'Seattle', 'Austin']
6. Parsing CSV data with Python string split() method
One of the most practical applications of the Python String split() Method is parsing CSV (Comma-Separated Values) data.
CSV files are widely used for storing tabular data such as user records, product catalogs etc. While Python provides a built-in
csv module for robust handling, the split() method offers a quick and lightweight way to process simple CSV strings.
data = "Name,Age,Location"
row = "Ram,30,New Delhi"
# Split header and row using comma as delimiter
headers = data.split(',')
values = row.split(',')
# Combine into dictionary for easy access
record = dict(zip(headers, values))
print(record)
Output
{'Name': 'Ram', 'Age': '30', 'Location': 'New Delhi'}
That's all for this topic Python String split() Method. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
-
Changing String Case in Python
-
Python count() method - Counting Substrings
-
Python Program to Reverse a String
-
Operator Overloading in Python
-
Abstract Class in Python
You may also like-
-
Magic Methods in Python With Examples
-
self in Python
-
Namespace And Variable Scope in Python
-
raise Statement in Python Exception Handling
-
PermGen Space Removal in Java 8
-
String join() Method And StringJoiner Class in Java
-
Spring Web MVC Java Configuration Example
-
Data Compression in Hadoop