Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Python String split()

In Python, String split() is an inbuilt function that is used to split a string into a list. Sometimes there is a need to break the large string into small pieces.

  • This method breaks the given string by a specified separator(separator is a specified character used to place between each item) and returns the list of strings.

  • The default separator used to separate the string is whitespace; though you can also specify any separator.

  • With the help of String split() , one of the essential operations on the string i.e splitting the string can be done in a flexible way.

  • Basically splitting is opposite to merging; merging is done using string concatenation() while splitting is done using string split()

Python String split(): Syntax

Below we have a basic syntax of String split() in Python:

str.split(separator, limit)

Python String split(): Parameters

Given below is a description of Parameters of split() as there are two parameters:

  • separator:

The separator parameter of split()is used to provide the delimiter which is then used to split the string from a specified character.

  • limit:

The limit parameter of split() is the optional limit if it is provided, then it splits the string into the maximum of the provided number of times.

Python String split(): Basic Example

Below we have an example to show the working of String split() function:

a = "k u n a l"
data = a.split()

for char in data:
    print(char)

In the above example, we have not specified any delimiter thus whitespace is used as a default separator in this case. The output for the same is given below:


k
u
n
a
l

String split(): with no Parameters provided

strA = 'Welcome! to StudyTonight'
splitA = strA.split()
print(splitA)

In the above example, we have not provided any parameter inside the split() function and the output for the same is given below:


['Welcome!', 'to', 'StudyTonight']

String split(): with Parameters provided

strA = 'Welcome! to our StudyTonight website'
splitA = strA.split('!')
print(splitA)

In the above example, we have provided a separator; thus it splits the strA into two from the separator(!) and output for the same is given below:


['Welcome', ' to our StudyTonight website']

String split(): with Parameters and maxsplit or limit is provided

Below we have an example where we have provided with both the parameter of String split() and When the limit parameter of split() is specified, the list will contain the specified number of items plus one:

strA = 'Welcome! to our StudyTonight website'
splitA = strA.split(' ',4)
print(splitA)

In the above example, we have put the value of the limit parameter equals to 4. That is why we get the list of 5 items. Output for the same is given below:


['Welcome!', 'to', 'our', 'StudyTonight', 'website']

Splitting Python String into Dictionary using String split()

Now we are going to split a Python String into a Dictionary; The code snippet for the same is given below:

str="a=him;b=duck;c=john"
dictRes=dict(item.split("=") for item in str.split(";"))
print(dictRes)

The output for the above code snippet is shown below as it will convert the given string into the respective dictionary:


{'a': 'him', 'b': 'duck', 'c': 'john'}

Splitting Python String into integers

Now we are going to separate a string into a respective list of integers and the code snippet for the same is given below:

str="11 34 56 67 89"
mp = map(int, str.split())
res = list(mp)
print(res)

Here we have also used map() and int() function of python; because we need to map objects to a list using list() method. The output for the same is given below:


[11, 34, 56, 67, 89]

Time For Live Example!

Now it's time to take a look at the live example of split() example:

Summary

In this tutorial, we learned the split() method of strings in Python which is used to split a string. We saw a few examples for the same; we had also converted a string into integers and string into a dictionary further followed by a Live Example.