Python String Split Method


title: String Split Method

The split() function is commonly used for string splitting in Python.

The split() method

Template: string.split(separator, maxsplit)

separator: The delimiter string. You split the string based on this character. For eg. it could be ” “, “:”, “;” etc

maxsplit: The number of times to split the string based on the separator. If not specified or -1, the string is split based on all occurrences of the separator

This method returns a list of substrings delimited by the separator

Examples

1) Split string on space: ” “

string = "Codevarsity is fun." print(string.split(" "))

Output:

['Codevarsity', 'is', 'fun.']

2) Split string on comma: “,”

string = "Codevarsity,is fun, and informative" print(string.split(","))

Output:

['Codevarsity', 'is fun', ' and informative']

3) No separator specified

string = "Codevarsity is fun and informative" print(string.split())

Output:

['Codevarsity', 'is', 'fun', 'and', 'informative']

Note: If no separator is specified, then the string is stripped of all whitespace

string = "Codevarsity is fun and informative" print(string.split())

Output:

['Codevarsity', 'is', 'fun', 'and', 'informative']

3) Split string using maxsplit. Here we split the string on ” ” twice:

string = "Codevarsity is fun and informative" print(string.split(" ", 2))

Output:

['Codevarsity', 'is', 'fun and informative']

More Information

Check out the Python docs on string splitting

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.