The split()
is a method in Python that is used to split a string into a list of substrings. It takes two optional arguments: First, sep
, which is a string delimiter that separates the substrings (defaults to while space), and second, maxsplit
which is an integer that specifies the maximum number of splits to perform.
Key Points of Split String in Python:
split()
is used to split the string on the specified delimiter.- Returns the list of substrings.
- If
sep
is not specified, the string is split on whitespace characters. - If
maxsplit
is not specified, the string is split into all possible substrings
In this article, we will discuss several ways to split the given string in different forms using the split() method in Python. The string is a set of characters similar to C, C++, and other programming languages.
1. Quick Examples of String Split
Following are quick examples of how to split a string.
# Consider the string
st1="Hello-welcome-to-sparkby-examples"
print("String: ",st1)
# Split the string using sep parameterr.
print(st1.split("-"))
# Split by using sep & maxsplit as 2.
print(st1.split("-",2))
# Consider the string
st1="Hello"
# Split the string by using list()
print(list(st1))
# Split the string by using tuple()
print(tuple(st1))
2. String split() in Python
The split() in python is a built-in function that is used to split the string by a delimiter into several substrings. It will take sep
as the first parameter that specified the delimiter to split on and maxsplit
as a parameter that takes an integer value to specify a maximum number of splits to be done.
2.1 split() syntax
Following is the syntax.
# Syntax
string1.split(delimiter,maxsplit)
2.2 split() parameters
- The first parameter takes a delimiter in which the string is split based on this delimiter.
- The maxsplit takes an integer value that specifies a maximum number of splits to be done. If it is not specified, it splits the string by all possible delimiters.
2.3 Python String split() Examples
Let’s see how to split the string by using the different combinations of the parameters.
2.3.1 split() with space delimiter
As I mentioned above, the default value of sep
is space hence, if you don’t specify the sep, it splits the string by space. however, in the below example, I have used the sep with ” ” as a param to explain.
# Consider the string
st1="Hello welcome to sparkby examples"
print("String: ",st1)
# Split the string by using space as a separator
splitted = st1.split(" ")
print(splitted)
Yields below output.
Here, we didn’t specify the maxsplit
parameter hence, it split string by all spaces and returns all substrings as a list.
2.3.2 split() with maxsplit
Let’s use another string with -
as a separator and use both sep
and maxsplit
parameters. In the first example below, the string is split on the first '-'
character, resulting in a list with two elements. In the second example, the string is split on the first two '-'
characters, resulting in a list with three elements.
# Consider the string
st1="Hello-welcome-to-sparkby-examples"
print("String: ",st1)
# Split the string by using - as a separator with maxsplit as 1.
print(st1.split("-",1))
# Split the string by using - as a separator with maxsplit as 2.
print(st1.split("-",2))
# Output:
# String: Hello-welcome-to-sparkby-examples
# ['Hello', 'welcome-to-sparkby-examples']
# ['Hello', 'welcome', 'to-sparkby-examples']
3. Split the String with substring
In all the above examples, we have used the single-character delimiter to split the string in python. The sep
param can also take a string as a value. so let’s split the string by substring and see how it behaves.
Here, we are splitting the string with a delimiter ‘welcome’, The final list has 3 strings.
# Consider the string
st1="Hello welcome to sparkby welcome examples"
print("String: ",st1)
# Split the string by using "welcome" as a separator
splitted = st1.split("welcome")
print(splitted)
# Output:
# String: Hello welcome to sparkby welcome examples
# ['Hello ', ' to sparkby ', ' examples']
4. Split the String using list()
The list() is the inbuilt method in python that can be used to split the string by each character. In the below example, the total number of characters in the actual string is 5, when we applied list() function, It is splitted into 5 characters and returned as a list.
# Consider the string
st1="Hello"
print("String: ",st1)
# Split the string by using list()
splitted = list(st1)
print(splitted)
# Output:
# String: Hello
# ['H', 'e', 'l', 'l', 'o']
5. Split the String using tuple()
The tuple() is an inbuilt method in python that will split the string by each character into tuple. In the below example, the total number of characters in the actual string is 5, when we applied tuple() function, It is splitted into 5 characters and returned in tuple.
# Consider the string
st1="Hello"
print("String: ",st1)
# Split the string by using tuple()
splitted = tuple(st1)
print(splitted)
# Output:
# String: Hello
# ('H', 'e', 'l', 'l', 'o')
6. Conclusion
In this article, you have learned the python split() method syntax, parameters, and how to split the string by delimiter. It takes two optional arguments: First, sep
, which is a string delimiter that separates the substrings (defaults to while space), and second, maxsplit
which is an integer that specifies the maximum number of splits to perform. In case if you wanted to split string by character use list() or tuple() methods.
Related Articles
- How to Parse a String in Python
- Compare strings using either ‘==’ or ‘is’ sometimes produces a different result.
- Python Extract Numbers From String
- Compare Strings in Python
- Check if String is Empty or Not in Python
- Convert Integer to String in Python
- Trim Whitespace from a String in Python?
- Convert String to Lowercase in Python
- Remove Special Characters From String Python
- F-strings in Python
- Python Create a long Multi-line String
- Convert String to Uppercase in Python
- Remove Character From String Python
- Python Convert Bytes to String