In this Python tutorial, I will explain, what is the string split() method in Python, and the syntax of the Python string split() method. And I will show a few examples of how to use the string split() method in Python.
Recently, while I was working on a Python application, I got a requirement to split a string using various methods in Python. Here I will explain everything about how to use the split() method.
Python String.Split() method
The split() method is a built-in string method in Python that allows you to split a string into a list of substrings based on a specified delimiter. The delimiter is the character or string that separates the individual substrings in the original string.
By default, the split() method in Python uses whitespace as the delimiter, which means that it will split the string into substrings whenever it encounters a space, tab, or newline character. However, you can specify a different delimiter if needed.
Syntax of string split()
here is the syntax for Python string.split() method.
myString.split('separator',maxsplit)
Here myString is the string that we want to split.
Here we use two parameters in the split method.
- separator: Into the split method, we can pass any delimiters like “*, /, &, (, ), {, }, \, |” etc. as a parameter. Also, we can use any characters or strings as separators for the split() method. If a separator is not provided for a given string then blank spaces are used as a separator by default.
myString.split('/',maxsplit)
- maxsplit: This is an optional parameter, that indicates how many times we need to split the string. It will accept a number as the value. And if no parameter is provided it takes default -1 i.e. there is no limit to split.
myString.split('/',2)
Read How to split a string into equal half in Python?
Python String split() example
Example 1: Split a string using whitespace as the delimiter
Let us see one example, of how to use the string split() method in Python.
# Defining a string
myStr="George has a Tesla"
#List of string
my_List=myStr.split()
print(my_List)
Output:
['George', 'has', 'a', 'Tesla']
Since we haven’t specified any delimiter, the split() method uses whitespace as the default delimiter and splits the string into four substrings.
Example 2: Split a string using split() with separators
Here we will see some examples of how the string split() works with different separators in Python:
Split using ‘,’ separator
#Defining a string
myStr="one, two, three, four, five"
#Split using ',' separator
num_List=myStr.split(',')
#Printing list of numbers
print(num_List)
# output
['one', ' two', ' three', ' four', ' five']
Split using ‘|’ separator
#Defining a string
myStr="Welcome|to|USA"
#split using '|' separator
my_List=myStr.split('|')
#prints list of strings
print(my_List)
Output:
['Welcome', 'to', 'USA']
Split using ‘-‘ separator
# Defining a string
state_names = "Florida-Texas-Calefornia-Ohio"
# Split using character '-'
state_name_list = state_names.split('-')
# Printing list of states
print(state_name_list)
Output:
['Florida', 'Texas', 'Calefornia', 'Ohio']
Example 3: Python string split() with maxsplit
In the Python string split() method, maxsplit takes numbers as parameters and it splits the string the equal number of times taken in the split() method with the given separator. The default maxsplit value is -1 means there’s no limit in split.
Split() method with default maxsplit: -1
# Defining a string
strNames="Tony, Steve, Peter, Bruce, Scott"
# default maxsplit: -1
print(strNames.split(','))
Output:
['Tony', ' Steve', ' Peter', ' Bruce', ' Scott']
Split() method with maxsplit: -1
# Defining a string
strNames="Tony, Steve, Peter, Bruce, Scott"
# maxsplit: -1
print(strNames.split( ',' , -1))
Output:
['Tony', ' Steve', ' Peter', ' Bruce', ' Scott']
As we can see in the executed codes below, both methods give us the same output as described.
let’s see a few more examples on maxsplit.
Split() method with maxsplit: 0
If we take 0 as a maxsplit parameter in split() method we will get the complete string as we have given 0 as a parameter and it will not split the string at all.
# maxsplit: 0
print(strNames.split(',', 0))
# output
['Tony, Steve, Peter, Bruce, Scott']
Split() method with maxsplit: 1
Similarly, if we take 1 as a maxsplit parameter with ‘,’ as a separator, the split() method will separate the string once, and further, the split() will not work.
# maxsplit: 1 print(strNames.split(',', 1))
Output:
['Tony', ' Steve, Peter, Bruce, Scott']
Split() method with maxsplit: 2
When we pass 2 as a maxsplit parameter with ‘,’ as a separator, the split() method will separate the string twice with ‘,’ separator and further the split() will not work.
# maxsplit: 2
print(strNames.split(',', 2))
Output:
['Tony', ' Steve', ' Peter, Bruce, Scott']
Split() method with maxsplit: 4
When we pass 4 as a maxsplit parameter with ‘,’ as a separator, the split() method will separate the string four times with ‘,’ separator. As our given input has total four ‘,’ it executes four times
# maxsplit: 4
print(strNames.split(',', 4))
Output:
['Tony', ' Steve', ' Peter', 'Bruce', 'Scott']
Conclusion
The split() method is a useful built-in string method in Python that allows you to split a string into a list of substrings based on a specified delimiter. This method can be handy when you need to work with individual words or tokens in a string or when you need to extract specific parts of a larger string.
In this blog post, we looked at the syntax of the split() method in python and provided several examples of how to use it in different scenarios. We saw how to split a string using whitespace as the default delimiter, how to specify a custom delimiter, and how to limit the number of splits using the maxsplit parameter in Python.
You may like the following Python tutorials:
- How to convert numpy array to list of strings in Python
- Python string uppercase()
- Python String format() Method
- Trim Whitespace from a String in Python
- How to split a string by comma in Python
- How to Split the Last Element of a String in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.