Python rsplit

Python rsplit string function is to split the given string and return a list of words, which is similar to Python Split function.

The Python rsplit function accepts two optional arguments. If you specify the first argument (separator), then the rsplit uses the specified separator to return a list of words. Python String 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, python uses Empty Space as the separator.
  • Max_Split: If you specify this value, then the rsplit function restricts the list of words.

Python rsplit String function returns a List of words. For example, If we have X*Y*Z and If we use * as a separator, rsplit function search 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 help 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 String Method separator, then Python returns the whole sentence as List Item 1.

Str1 = 'India, China, Japan, USA, UK, Australia, Canada'
Str2 = '[email protected]@[email protected]@[email protected]'
Str3 = '123 + abc + 456 + [email protected]'
 
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 ', ' [email protected]']
After Splitting Str 7 =  ['Tutorial', 'Gateway', 'Team']

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

Str4 = Str1.rsplit(',')

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

Str5 = Str2.rsplit('@')

The below statement used to split the Str3 string based on ‘+’ separator. Although it holds @ and ‘.’ symbols, it only split based on + symbol and prints the List of words output.

Str6 = Str3.rsplit('+')

Next, we tried to call the function without using any of the 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 Python split article too.

Python rsplit String Example 2

The following set of examples help 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 split the Str1 based on the separator we specified (i.e., ‘,’) and prints the output. Here, the second argument restricts to split one word only.

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

Within the last statement, we used the Python String rsplit directly. The following statement split the string 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