Python rsplit

Python rsplit string function is to split the given string and return a list of words, which is similar to split. The Python rsplit function accepts two optional arguments.

If you specify the first argument (separator), it uses the specified separator to return a list of words. The Python rsplit function starts looking for the separator from the Right-Hand side.

Python rsplit String Syntax

The syntax of the Python rsplit String function is

String_Value.rsplit(Separator, Max_Split)
  • Separator: if you forget this argument, it uses Empty Space as the separator.
  • Max_Split: If you specify this value, it restricts the list of words.

The Python rsplit String function returns a List of words. For example, If we have X*Y*Z and use * as a separator, it searches for * from right to left. Once it finds *, it returns the substring before the * symbol as List Item and so on.

Python rsplit String Example

The following set of examples helps you understand the Python rsplit string function. Here, we Pass either a single argument or No argument to the rsplit function.

NOTE: If you pass the non-existing item as the Method separator, Python returns the whole sentence as List Item 1.

Str1 = 'India, China, Japan, USA, UK, Australia, Canada'
Str2 = 'Find@Free@tutorials@on@Python@Programming'
Str3 = '123 + abc + 456 + xyz@yahoo.com'
 
Str4 = Str1.rsplit(',')
print("Splitting Str 1 from Right = ", Str4)

Str5 = Str2.rsplit('@')
print("Splitting Str 2 from Right = ", Str5)

Str6 = Str3.rsplit('+')
print("Splitting Str 3 from Right = ", Str6)

# RSplit function with No arguments
Str7 = 'Tutorial Gateway Team'

Str8 = Str7.rsplit()
print("After Splitting Str 7 = ", Str8)
Splitting Str 1 from Right =  ['India', ' China', ' Japan', ' USA', ' UK', ' Australia', ' Canada']
Splitting Str 2 from Right =  ['Find', 'Free', 'tutorials', 'on', 'Python', 'Programming']
Splitting Str 3 from Right =  ['123 ', ' abc ', ' 456 ', ' xyz@yahoo.com']
After Splitting Str 7 =  ['Tutorial', 'Gateway', 'Team']

The following statement splits the Str1 based on the specified separator (i.e., ‘,’) and prints the output.

Str4 = Str1.rsplit(',')

It splits Str2 based on the ‘@’ symbol and prints the output.

Str5 = Str2.rsplit('@')

The below Python rsplit statement separates the Str3 based on the ‘+’ separator. Although it holds @ and ‘.’ symbols, it only splits based on the + symbol and prints the List of words output.

Str6 = Str3.rsplit('+')

Next, we tried to call the function without using any arguments.

Str8 = Str7.rsplit()

From the Output, you can observe that it is splitting the string base on empty space because that is the default argument. Please refer split article too.

rsplit with two arguments

The following set of examples helps you understand the advanced String rsplit in Python Programming Language. Here, we pass either two arguments or No arguments.

First, we declared a sample text. The following Str3 statement splits Str1 based on the specified separator (i.e., ‘,’) and prints the output. Here, the second argument restricts splitting one word only.

The second argument restricts the rsplit function to split five words only. Let me show you the same in Str5.

Within the last statement, we used the rsplit function directly. The following statement separates the sentence based on the ‘/’ symbol and prints the output.

Str1 = 'India, China, Japan, USA, UK, Australia, Canada'

# with No arguments
Str2 = Str1.rsplit()
print("After Splitting String 1 = ", Str2)

# with No arguments
Str3 = Str1.rsplit(',', 1)
print("Splitting String 1 from Right = ", Str3)

Str4 = Str1.rsplit(',', 3)
print("Splitting String 1 from Right = ", Str4)

Str5 = Str1.rsplit(',', 5)
print("Splitting String 1 from Right = ", Str5)

#Performing directly
Str6 = 'https://www.tutorialgateway.org/python/'.rsplit('/', 2)
print("After Splitting String 6 = ", Str6)
Python RSplit String Example 2