Python - Split String - 3 Examples
Python Split String
To split a String in Python using delimiter, you can use string split() method. split() method splits the given string by a specified separator, into a list of values.
In this tutorial, we shall learn about the syntax of string split() method, and how to use this method to split a given string into a list of values, with well detailed example programs.
Syntax of string split() method
The syntax of String.split() method is
str.split(sep, maxsplit)
where
- str is the string which has to be split.
- sep is the separator/delimiter where the string is split. If not provided, white spaces are considered as separators.
- maxsplit is the maximum number of splits that can be done on the given string. If not provided, maximum possible splits are done.
Steps to Split String in Python
- Consider that we are given a string value in variable x.
- Call split() method on the string x, and pass the comma separator as argument.
- The split() method returns a list containing chunks created from the splitting of the given string by specified separator.
Examples
1. Split given string by comma in Python
In this example, we are given a string where the values in the string are separated by comma. We have to split this string using comma as separator using string split() method.
Program
The complete program to split the given string by comma separator using string split() method.
Python Program
# Given string
x = 'apple,banana,cherry,mango,fig'
# Split string by comma delimiter
values_list = x.split(',')
# Print split values
print(values_list)
Explanation
- The string
x = 'apple,banana,cherry,mango,fig'
is assigned to the variablex
. - The
split()
method is called on the stringx
, with a comma','
as the delimiter. This splits the string into a list wherever a comma is encountered. - The result of the split operation is stored in the variable
values_list
, which will contain the following list:['apple', 'banana', 'cherry', 'mango', 'fig']
. - The
print(values_list)
statement outputs the resulting list to the console, displaying:['apple', 'banana', 'cherry', 'mango', 'fig']
.
Output
['apple', 'banana', 'cherry', 'mango', 'fig']
2. Split string with a constraint on number of splits in Python
In this example, we take a string that has chunks separated by comma. We will split this string using comma as separator and maximum number of split operations to 3.
Python Program
# Given string
x = 'apple,banana,cherry,mango,fig'
# Split string with number of splits constrained to 3
values_list = x.split(',', 3)
# Print split values
print(values_list)
When there are splits done at three locations, we get four chunks.
Explanation
- The string
x = 'apple,banana,cherry,mango,fig'
is assigned to the variablex
. - The
split()
method is called on the stringx
, with a comma','
as the delimiter and3
as the maximum number of splits. This means the string will be split at the first three commas, resulting in a list with a maximum of four elements. - The result of the split operation is stored in the variable
values_list
, which will contain the following list:['apple', 'banana', 'cherry', 'mango,fig']
. - The
print(values_list)
statement outputs the resulting list to the console, displaying:['apple', 'banana', 'cherry', 'mango,fig']
.
Output
['apple', 'banana', 'cherry', 'mango,fig']
3. Call split() on string with no arguments passed in Python
When no arguments are given to split() function, one or more white spaces are considered as delimiter, and the input string is split.
In this example, we will split a string by space using default behavior of string split() method.
Python Program
# Given string
x = 'apple banana cherry'
# Default behavior of split() method
values_list = x.split()
# Print split values
print(values_list)
Explanation
- The string
x = 'apple banana cherry'
is assigned to the variablex
. - The
split()
method is called on the stringx
with no arguments. This means it will split the string at any whitespace (spaces, tabs, or newlines) by default. - The result of the split operation is stored in the variable
values_list
, which will contain a list of the words:["apple", "banana", "cherry"]
. - The
print(values_list)
statement outputs the resulting list to the console, displaying:['apple', 'banana', 'cherry']
.
Output
['apple', 'banana', 'cherry']
Summary
In this tutorial of Python Examples, we have gone through different scenarios where we split the string with different types of delimiters, controlled the number of splits, etc.