Python Split String - split() - Tutorial Kart

[Pages:4]Python Split String ? split()

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.

Syntax of split() function

The syntax of split() function is: string.split(separator, max)

where separator is the delimiter by which the string is split into parts. The datatype of separator is string. The default value is a white space. max is the maximum number of splits the string can be split into. By default the max is the upper limit of an integer, i.e., the default value does not limit the splits.

Both separator and max parameters are optional.

Example 1 ? 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)

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)

Output

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

Example 2 ? 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)

Output

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

Example 3 ? 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)

Output

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

Example 4 ? 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)

Output

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

Example 5 ? 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)

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.

Python Programming

Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments

Control Statements

Python If Python If Else Python While Loop Python For Loop

Python String

Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings

Functions

Python Functions

Python Collections

Python List Python Dictionary

Advanced

Python Multithreading

Useful Resources

Python Interview Questions

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download