Methods and Functions

Python String Split()

We all know how splitting plays a crucial role, especially while distributing food or other things between our friends and family members. In the same way, we will need to split some strings while programming based on the context of the problem. So, to do this job, we can use the Python split() method. Now, let us get into the details of how this string split() method works and how to use it. 

Definition of Python Split

The split() method in Python divides up a string into a list of strings after splitting the given string by the specified separator. 

Syntax: 

 The syntax of the split() method: str.split(separator, maxsplit) 

python split

                                                                                                                                                 Source

Parameters 

 The split() method in Python takes in a maximum of two parameters, which are: 

  1.  Separator: This is a delimiter. It specifies the split() method at which point the string should be split. If this parameter is not provided then the split() method considers any white space as a separator. 
  2.  Max split: It is an integer, which instructs the split() method to break up the string into a specified number of times only. If this parameter is not provided then by default the split() method takes no limit. 

Return value: 

The split() method will return a list of strings after splitting the string based on the given separator. 

Now, let us look at some examples to understand how the Python split() method works.

                    

text = 'Hi how are you' 

# Splits at space
print(text.split())

word = 'Hi, How, are, you' 

# Splits at ','
print(word.split(','))

word = 'Hi:How:are:you'

# Splitting at ':'

print(word.split(':'))

word = 'CagBagSaFagor'

# Splitting at g

print(word.split('g'))

 Output: 
                    

[‘Hi’, ‘How’, ‘are’, ‘you’] 

[‘Hi’, ‘ How’, ‘ are’, ‘ you’]

[‘Hi’, ‘How’, ‘are’, ‘you’] 

['Ca', 'Ba', 'Sa', 'Fa', 'Or']

In the first line of output, we can see that the text was split at all the white spaces since we didn’t specify the separator value. 

In the second line of output, we can see that the text was split at all the ‘,’, since we specified the separator value as ‘,’. 

In the third line of output, we can see that the text was split at all the ‘:’, since we specified the separator value as ‘:’. 

In the last line of output, we can see that the text was split at all the places where the ‘g’ alphabet was present since we specified the separator value as g. 

Example 2: How split() works when max split is specified?

 Now, let us look at some examples to understand how the split() method works when max split is specified. 

                    

word = 'My, name, is, Pawan'  

# maxsplit: 0

print(word.split(', ', 0)) 

# maxsplit: 4

print(word.split(', ', 4))

# maxsplit: 1

print(word.split(', ', 1))

Output: 
                    
 ['my, name, is, pawan']

['my', 'name', 'is', 'pawan']

['my', 'name, is, pawan']

In the first line of output, we can see that the word is not split because we have given the max split values as 0. 

In the second line of output, we can see that the word was split 4 times at the given specified separator ‘,’ since we have given the max split value as 4. 

In the third line of output, we can see that the word was split once at the specified separator ‘,’ since we have given the max split value as 1. 

FAQs on  Python Split

Now, to give you a quick brief about how the Python split() method works and how to use it, we have answered some frequently asked questions for you, which are: 

Q1. What does split () do in Python?

A. The split() method in Python takes in two parameters and based on the parameters provided it splits a string at the and returns a list of strings. 

 Q2. How do you split text in Python?

A. In Python, we split the text using a method named string split(), which breaks up the given string at the intervals and number of times specified by us. 

 Q3. How do you split a word in a string in python?

A. In Python, we can split a word in a string by using the string split() method. We must give the starting character before the word which we need to split as the separator value and 1 as the max split value to split a word in a string.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.