Python String rsplit() Method: Complete Guide

0
23
Python String rsplit() Method Example

The rsplit() method returns a list after dividing the string based on the delimiter(separator). Giving separator as an argument is optional. If we don’t provide any separator, whitespace is taken as a separator by default.

Python String rsplit()

To split the string based on the operator in Python, use string.split() method. Python string rsplit() is a built-in method that separates the string and returns the list. It splits from the right using the separator as a delimiter. If the separator is not specified, any whitespace string is a separator.

Syntax

str.rsplit(separator, max)

Here str is the string variable that holds the main string on which we will use the rsplit() method.

Parameter

There are two parameters passed in the rsplit() method.

First is the separator; based on this separator, the rsplit() method splits the string from the right side.

The second is the max, and it fixes how many times we want to split the string. 

If it is not mentioned, then the string is split until the separator is found in the string.

Return Value

It returns a list with the split values of the string.

Example programs on rsplit() method in Python

Example 1: Write a program to show the working of the rsplit() method.

# app.py

h1 = "Hello its sunny today"
h2 = "It's rainy at the same time"
h3 = "I love coding in this weather"
h4 = "I also love learning from AppDividend"
h5 = "Okay Bye I have to study!"
print("String before split: ", h1)
print("String after split: ", h1.rsplit("s", 2), "\n")
print("String before split: ", h2)
print("String after split: ", h2.rsplit("t", 2), "\n")
print("String before split: ", h3)
print("String after split: ", h3.rsplit("g"), "\n")
print("String before split: ", h4)
print("String after split: ", h4.rsplit("f", 2), "\n")
print("String before split: ", h5)
print("String after split: ", h5.rsplit("!", 1), "\n")

Output

String before split:  Hello its sunny today
String after split:  ['Hello it', ' ', 'unny today'] 

String before split:  It's rainy at the same time
String after split:  ["It's rainy at ", 'he same ', 'ime'] 

String before split:  I love coding in this weather
String after split:  ['I love codin', ' in this weather'] 

String before split:  I also love learning from AppDividend
String after split:  ['I also love learning ', 'rom AppDividend'] 

String before split:  Okay Bye I have to study!
String after split:  ['Okay Bye I have to study', '']

Example 2: Write a program to split the string 2 times and based on whitespace character.

# app.py

h1 = "hello once again my friends!!"
print("String before split: ", h1)
print("String after split: ", h1.rsplit(" ", 2))

Output

String before split:  hello once again my friends!!
String after split:  ['hello once again', 'my', 'friends!!']

Difference between split() and rsplit():

They both function the same the only difference is that rsplit() splits the string from the right side, which is not the case with split(). It splits from the first occurrence. 

That’s it for the rsplit() function in Python.

See also

Python string rindex()

Python string rfind()

Python string swapcase()

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.