Methods and Functions

Python String rsplit()

We all know that Python provides us with the String split() function that inputs a string and returns a list that contains words separated based on the separator and maxsplit parameters given. Python also provides us with another split() function i.e. Python rsplit() function which operates exactly like the split() but starts separating from the right side of the input string. Let us learn more about Python rsplit() in the article below.

Python rsplit() function

Definition

  • Python rsplit() is a string method that is used to return a list of strings by splitting the input string from the right at the specified separator.
  • The Python rsplit() string function is used to break the string into parts from the right side of the string on the basis of the specified separator.

Python rsplit() Syntax

The Python rsplit() function follows the below-mentioned syntax:

                    

string.rsplit(separator, maxsplit)

rsplit() Parameters

Python rsplit() method accepts 2 optional parameters:

  • separator (Optional) – The rsplit() method splits a string starting at the right and ending at the provided separator. If no separator is specified, any whitespace string (space, newline, etc.) acts as a separator.
  • maxsplit(Optional) – It is a number that tells us to split the string as many times as the number provided. There is no limit if this parameter is not supplied. The list contains maxsplit+1 number of elements.

Return value from rsplit()

The rsplit() function splits the string at the separator, beginning from the right side, and returns a list of strings.

Example 1: How rsplit() works in Python?

The following example shows how to use the Python rsplit() method, which is the same as the split() method i.e. when maxsplit parameter is not specified, rsplit() acts like split() function.

Example

                    

# Python program to illustrate rsplit()
msg = 'I love to travel places'
# splits at space
print(msg.rsplit())
print(msg.rsplit(' '))

# splits at \n
print('I\nam\na\nfoodie'.rsplit('\n'))

# splits at \t
print('I\tam a\tfoodie'.rsplit('\t'))

Output

                    

['I', 'love', 'to', 'travel', 'places']
['I', 'love', 'to', 'travel', 'places']
['I', 'am', 'a', 'foodie']
['I', 'am a', 'foodie']

All of the strings in the preceding example splits at the default whitespace characters such as ‘ ’, ‘\t’, and ‘\n’. The rsplit() function also splits the Unicode character ‘\u2028’ used as a line separator.

Example 2: rsplit() with various separators

Example

                    

# Python program to illustrate rsplit()
my_list = 'Egg, Bread, Butter, Cheese, Milk'
# splits when , and whitespace found together
print(my_list.rsplit(', '))

names = 'Tim:Sam:Jim:Adam'
# splits at :
print(names.rsplit(':'))

msg = 'P||Y||T||H||O||N'
print(msg.rsplit('||'))

sent = 'I Plove Ppython Pprogram'
print(sent.rsplit(' P'))

Output

                    

['Egg', 'Bread', 'Butter', 'Cheese', 'Milk']
['Tim', 'Sam', 'Jim', 'Adam']
['P', 'Y', 'T', 'H', 'O', 'N']
['I', 'love', 'python', 'program']

Example 3: How split() works when maxsplit is specified?

Example

                    

# Python program to illustrate rsplit()
lang = 'Python,C,C++,R,Java,SQL,Perl'
print(lang.rsplit(',', 2))
print(lang.rsplit(',', 4))
print('')

places = 'Tokyo Delhi Paris Brazil Mumbai'
# separator is None
# string will be splitted at space
print(places.rsplit(None, 1))
print(places.rsplit(None, 3))

Output

                    

['Python,C,C++,R,Java', 'SQL', 'Perl']
['Python,C,C++', 'R', 'Java', 'SQL', 'Perl']

['Tokyo Delhi Paris Brazil', 'Mumbai']
['Tokyo Delhi', 'Paris', 'Brazil', 'Mumbai']

Explanation

In the first print statement, the lang.rsplit(‘,’, 2) specifies 2 as maxsplit argument, so it will split lang string 2 times from the right and so the output list will contain 3 elements.

Frequently Asked Questions

Q1. What is rsplit in Python?

Python rsplit() is a string function that operates exactly like the split() but starts separating from the right side of the input string. Python rsplit() is a string method that is used to return a list of strings by splitting the input string from the right at the specified separator.

Q2. What is the difference between split and rsplit in Python?

The Python split() and rsplit() are exactly the same functions. The rsplit() method works in the same way as the split method in that it splits a string from the specified separator and produces a list object containing string elements. Any whitespace character, such as space, \t, \n, and so on, is used as the separator by default.

The only difference between split() and rsplit() is the use of the maxsplit argument. If the maxsplit argument is set, the rsplit() function splits a string from the right side (from the final character), whereas the split() method splits from the left side (from the first character).

Example

                    

my_str = 'Today is the first day of school'
# split() and rsplit() without any parameters
print(my_str.split())
print(my_str.rsplit())
print('')

my_str = 'I am a student who is currently pursuing MBA'
# split() and rsplit() with parameters
print(my_str.split(None, 3))
print(my_str.rsplit(None, 3))

Output

                    

['Today', 'is', 'the', 'first', 'day', 'of', 'school']
['Today', 'is', 'the', 'first', 'day', 'of', 'school']

['I', 'am', 'a', 'student who is currently pursuing MBA']
['I am a student who is', 'currently', 'pursuing', 'MBA']

Q3. How do you split words in Python?

We can split words in Python by using 2 built-in functions known as the Python split() function and the Python rsplit() function.

The difference between split() and rsplit() is the use of the maxsplit argument. If the maxsplit argument is set, the rsplit() function splits a string from the right side (from the final character), whereas the split() method splits from the left side (from the first character).

The syntax for both the functions are as below:

                    

string.split(separator, maxsplit)

string.rsplit(separator, maxsplit)

Example

                    

string = 'Hey how are you?'
print(string.split(' '))

my_str = 'I love Python programming !!!'
print(my_str.rsplit())

Output

                    

['Hey', 'how', 'are', 'you?']
['I', 'love', 'Python', 'programming', '!!!']

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.