How to use Split in Python?

By | September 27, 2021
How to use Split in Python_

Python is one of the most popular High-level programming languages and like other programming languages, it comes with various data types including Python String. Despite being an immutable data structure, we can manipulate the python string using some in-built python methods like split(). Here in this Python tutorial, you will learn how to use the Python split method or function to manipulate a string object.

By the end of this article, you will have a brief idea about

  • Python String
  • What is a split function in Python
  • how to use the Python split() function.
  • why do we need split() Python method
  • python split method parameters

Before jumping to the meat of this article let’s discuss what is a string in python because before you learn about thesplit() python method or function you should know about the python string.

What is a string in Python?

A Python string is a representation of Unicode character values inside a double or single quote.

Python string store its character in sequential order and follow the concept of indexing to access individual character. Like the Python list, the Python sting also supports slicing, however, due to its immutable nature, we cannot perform index value assigning with Python string.

Vamware

Example

>>> string = "TechGeekBuzz.Com"

>>> print(string)
TechGeekBuzz.Com

>>> print(string[0])  #string indexing
T

>>> print(string[0:6])  #string slicing
TechGe
>>> string[0] ="H"  #error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

What is split() function or method in Python?

Python string is a Data object and like other objects, it comes with some built-in methods. Python spilt() is one of the Python strings methods which return a list of sub-strings by dividing the string based on the given separator.

Vamware

By default, the split() method divides the string based on the empty space and return a list of string words.

Python split() method syntax

string.split()

Python split string Example

>>> string = "word1 word2 word3 word4"
>>> split_list = string.split()   
>>> split_list
['word1', 'word2', 'word3', 'word4']

In the above example, you can see that the split() method returns a list of sub-strings by diving the string based on the white space.

By default, the spilt() method divides the string into a list based on the white space between the words, but this behavior of the spilt() method can be changed by passing the separator parameter. Before we discuss the spilt() method parameters let’s discuss why do we use split() method.

Why do we need spilt() method?

  • Using the python split string’s function, we can divide the string into sub-strings.
  • It can be useful to sort the string based on the words.
  • The split() python method comes very useful during string analysis.
  • it also helps in decode encrypted strings.

Python spilt() method parameters

The spilt() method can accept two parameters and both the parameters are optional. The first parameter of split() method is separator and the second parameter is max.

Syntax:

string.split(separator, max)

Separator:

The Separator parameter defines the delimiter, on which basis the division of the string takes place. By default the separator value is a single white space, that’s why split() method divides the string based on the single white space between two characters.

Example

>>> string1 = "Word1,Word2,Word3,Word4"
>>> string1.split(",")   #divide the string based on ,
['Word1', 'Word2', 'Word3', 'Word4']


>>> string2 = "Word1-Word2-Word3-Word4"
>>> string2.split("-")    #divide the string based on -
['Word1', 'Word2', 'Word3', 'Word4']

Max

The max parameter defines the number of splits that should take place by the split() method. By default, its value is -1 which represents no-limit of splitting.

Example

>>> string1 = "Word1,Word2,Word3,Word4"

>>> string1.split("," , 2)  #only 2 split based on ,
['Word1', 'Word2', 'Word3,Word4']

In the above example using the split("," , 2) statement we specify split only up to 2 (,) separator.

How to use the python split() method Examples

Example 1: split() method can be used with input() method for List or tuple unpacking

The split() method is often used with input() function, for list unpacking.

First_Name, Last_name, Age = input("Enter Your full name with age: ").split()
print("First Name: ",First_Name)
print("Last Name: ",Last_name)
print("Age: ",Age)

Output

Enter Your full name with age: Ram Kumar 49
First Name:  Ram
Last Name:  Kumar
Age:  49

Example 2: Accept numeric items for a list and convert input string values into integer

If you want to insert numerical values for a list from the user then split() method can be used along with map() function to convert the user entered numeric string value to integer value.

string = input("Enter numerical values: separated by white space: ")
int_array = list(map(int, string.split()))
print("The string: ", string)
print("The int_array: ", int_array)

Output

Enter numerical values: separated by white space: 23 35 763 27 38
The string:  23 35 763 27 38
The int_array:  [23, 35, 763, 27, 38]

Conclusion

Here in this tutorial, you learned about string and string’s split function in Python. Here you also learned about its two optional parameters separator & max, and how to use split() method in Python. For most of the cases, you will be using split() method without specifying any separator value, however, for some cases, you might need to specify the separator delimiter.

People are also reading: 

Author: Vinay

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published. Required fields are marked *