Strings are an important data type and are used to store information in a system. When programming, you may need to break down a string into multiple chunks to get the most important information from a large block of characters. A function or built-in mechanism is necessary, in this case, to split a string into multiple parts.
Python provides the built-in split() function to split strings into separated pieces. The split() function separates a string into multiple strings, arranges them in a list, and returns the list. The split() function breaks down or splits the string according to a defined separator, which can be any special character (“,”, “:”, “@”, etc.).
This article describes the Python split() function with some examples of its usage.
Syntax
The split() function syntax is as follows:
The split() function takes two parameters as an argument, i.e., separator and maxsplit. Both parameters are optional. The separator splits the string. If you do not specify any separator, then the split() function will split the string on the basis of white space. The maxsplit argument is used to specify the number of splits and its value is -1 by default. This means that the function will split the string into multiple chunks.
Let us look at some examples using the split() function.
Examples
First, we will see an example where the string is split based on white space. The split() function divides the string and returns it in a list.
str="Hello and welcome to the linuxhint"
# using the split() function
print(str.split())
Output
You can see in the output that the split() function has returned the separated string in the form of a list.
Next, we will use a separator to split a string.
Using ‘,’ as a Separator
In this example, we will split the string using the ‘,’ separator.
str="Hello, and, welcome, to, the, linuxhint"
# using the split() function
print(str.split(","))
Output
Using ‘:’ as a Separator
Next, we will split a string using the “:” separator.
str="Fog computing: A new distributed computing paradigm"
# using the split() function
print(str.split(":"))
Output
Similarly, you can use any special character or word as a separator to split a string.
Using ‘@’ as a Separator
Now, we will split a string using ‘@’ as a separator. For example, if you have an email and you only want to get the domain of the email, in this case, you would use ‘@’ to split the string.
str="[email protected]"
# using the split() function
print(str.split("@"))
Output
Using ‘#’ as a Separator
In this example, we will separate the string using the ‘#’ separator.
str="cat#dog#cow#elephant#camel#lion#horse"
# using the split() function
print(str.split("#"))
Output
The maxsplit Parameter
The maxsplit parameter is optional. The maxsplit parameter is used to specify the maximum number of splits made by the function. The default value of the maxsplit parameter is -1. In this example, the value of the maxsplit parameter is 1, so the function will only separate out the first part of the string.
str="cat#dog#cow#elephant#camel#lion#horse"
# using the split() function with maxsplit 1
print(str.split("#",1 ))
Output
Now, let us change the value of the maxsplit parameter to 2. See the output below:
str="cat#dog#cow#elephant#camel#lion#horse"
# using the split() function with maxsplit 1
print(str.split("#",2 ))
Output
Now, let us change the value of the maxsplit parameter to 2. See the output below:
str="cat#dog#cow#elephant#camel#lion#horse"
# using the split() function with maxsplit 1
print(str.split("#",6))
Output
Conclusion
This article describes the usage of the Python split() function with the support of a few simple examples. The Python split() function splits strings based on separators and parameters and returns an output in the form of a list. The Spyder3 editor was used for creating and writing the Python scripts in the examples provided in this article.