Split String in Python

To split a String in Python with a delimiter, use split() function.

split() function splits the string into substrings and returns them as an array.

In this tutorial, we will learn the syntax of split() function, and how to use this function to split a string based on the delimiter string and maximum number of splits that can be made.

Syntax of split()

The syntax of split() function is

string.split(separator, max)

where

ParameterOptional/MandatoryDescription
separatorOptionalA string. Default value is white space. This is the delimiter by which the string is split into parts.
maxOptionalAn integer. This is the maximum number of splits the string can be split into. Default value does not limit the splits.

Return Value

A list.

Examples

Split String with White Space as Separator

In this example, we take a string and split it into multiple strings with space as separator.

The default value of separator parameter is a white space, hence we shall not pass any parameters to split() function.

Example.py

#a string
str = "Welcome to Python Tutorial by TutorialKart"

#split with no separator passed
parts = str.split()

print(parts)
Try Online

Output

['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']

We can also pass white space as separator.

Example.py

#a string
str = "Welcome to Python Tutorial by TutorialKart"

#split with single space as separator
parts = str.split(' ')

print(parts)
Try Online

Output

['Welcome', 'to', 'Python', 'Tutorial', 'by', 'TutorialKart']

Split String with Comma as Separator

In this example, we take a string and split it into multiple strings with comma as separator.

This example demonstrates how to extract values as an array when the input is a comma separated string.

Example.py

#a string
str = "0.124,0.547,4.125,1.2,10.63"

#split with comma as separator
values = str.split(',')

print(values)
Try Online

Output

['0.124', '0.547', '4.125', '1.2', '10.63']

Split String with another String as Separator

In this example, we take a string and split it into multiple strings with another string as separator.

Example.py

#a string
str = "hello---world---welcome---to---tutorialkart"

#split with another string as separator
values = str.split('---')

print(values)
Try Online

Output

['hello', 'world', 'welcome', 'to', 'tutorialkart']

Split String with Separator at the end of String

If you have the separator at the very start of this string or at the very end of this string, the output contains an empty element at the start or end of the array respectively.

Example.py

#a string
str = "---hello---world---welcome---to---tutorialkart---"

#split with another string as separator
values = str.split('---')

print(values)
Try Online

Output

['', 'hello', 'world', 'welcome', 'to', 'tutorialkart', '']

Split String with Maximum Number of Splits Specified

If you specify the max parameter, the split() method consider only the first max number of separators in the given string.

Example.py

#a string
str = "hello---world---welcome---to---tutorialkart"

#split with another string as separator
values = str.split('---', 2)

print(values)
Try Online

Output

['hello', 'world', 'welcome---to---tutorialkart']

Conclusion

In this Python Tutorial, we learned to split a string in Python using separator and also with the exception of maximum number of splits.