Strings are a great tool in the hands of Python developers. In Python, a string is a sequence of characters in quotation marks. It can include numbers, letters, and characters. With Python, a string can be divided into a list of substrings by a list of strings by a specific delimiter. This is done using the split
method. In this article, let’s look at how to use it.
Table of Contents
What does split do in Python?
The split
function scans the entire string and splits it if it finds a delimiter. A string must have at least one delimiter. This may include including spaces. Space is the default delimiter. If the parameter is not set, then the division will be carried out exactly by the space character space character. The syntax of the function is as follows: string.split(separator*, maxsplit*)
The
separator
parameter is optional, but it allows you to set the separator manually. The maxsplit
parameter defines the maximum number of separations. The default value is -1, all splits will be done.
How to split a string in Python
The .split()
method splits the main string by a separator and returns a list of strings.
In the example above, the string
my_st = "Example Python string."
print(my_st.split())
my_st
was declared. It is placed in single quotes. The .split()
function splits it into a list of such strings: ['Example', 'strings', 'Python']
The output contains a list of substrings. Another example of string partitioning:
In the above example we created a string
my_st = "blue,orange,red"
print(my_st.split(","))
my_st
with 3 substring. In this case it is the comma that will act as the split parameter in the function. The output will be as follows: ['blue', 'orange', 'red']
Examples of line breaks in Python
Separating terms by space
If you don’t pass a separator parameter, .split()
will perform a space division.
The code will return:
my_st = "Example Python string."
print(my_st.split())
['Example', 'string', 'Python']
. Note that we didn’t specify a delimiter to use when calling .split()
, so we use a space as the delimiter.
Splitting a string by a comma
A comma (","
) can act as the separator. This will return a list of strings that were originally surrounded by commas.
The output is
my_st = "For example, the Python string"
print(my_st.split(","))
['For example', ' Python string']
. The result is a comma-separated list of substrings in the original string.
Splitting strings by multiple delimiters
You can even use multiple delimiters in Python. To do this, you simply need to pass several characters as separators to the split function. Let’s take as an example a situation where :
and ,
are separators at the same time. Let’s use the function re.split()
.
Output:
import re
my_st = "I\puchu; programming language\nPython"
print(re.split(";|,|\n", my_st))
['me', 'learning', 'language', 'programming', 'Python']
Here we use the re module and regular expression functions. The variable
my_st
has been assigned a string with several delimiters, including “\n”, “;”, and “,”. And re.split()
is called for that string with the above delimiters. The output is a list of substring splits based on the original string.
How does the maxsplit parameter in the split function work?
This parameter helps to set the maximum number of splits. You may split a string by passing the value of this parameter. For example, if the separator is a space character, and maxsplit
is 1
, then the string will be divided into 2 substring at most.
The string
languages = "Python,Java,Perl,PHP,Swift"
print(languages.split(",",1))
languages
stores a string listing different languages. The split
function takes a comma as a separator and a value of 1 for the maxsplit
parameter. This means that the split will only be done once. ['Python', 'Java,Perl,PHP,Swift']
The following example shows how to execute the split twice. Here, the separator is a space, and the value of maxplit
is 2.
languages = "Python,Java,Perl,PHP,Swift"
print(languages.split(",",2))
['Python', 'Java', 'Perl,PHP,Swift']
How to split a string in the middle
The .split()
function cannot split a string into two equal parts. However, you can use slices (operator :
) and the len()
function to do so.
Output:
languages = "Python,Java,Perl,PHP,Swift"
mean_index = len(languages) // 2
print(f "First half: {languages[:mean_index]}")
print(f "Second half: {languages[mean_index:]}")
First half: Python,Java,P
The value of
Second half: erl,PHP,Swift
languages
was divided into two equal parts. The integer division operator was used for the work.
Output
Here’s what you learned:
- The
split
function splits a string into substrings by a delimiter. - The
maxsplit
parameter allows you to specify the maximum number of splits. - If you don’t specify a separator, the default value is space.
- Splits are used to divide strings into equal parts.