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.

Syntax of a Python rsplit String Function

The syntax of the Python String rsplit function is

String_Value.rsplit(Separator, Max_Split)
  • String_Value: A valid String variable, or use the String directly.
  • Separator: This argument is optional, and if you forget this argument, python uses Empty Space as the separator.
  • Max_Split: This argument is optional. If you specify this value, then the rsplit function restricts the list of words.

Python String rsplit 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 string before the * symbol as List Item and so on.

Python rsplit String Example

The following set of examples help you understand the String rsplit in Python Programming Language. 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 string as List Item 1.

# Python RSplit Method Example
 
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 String 1 from Right = ", Str4)

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

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

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

Str8 = Str7.rsplit()
print("After Splitting String 7 = ", Str8)

String rsplit function output

Splitting String 1 from Right =  ['India', ' China', ' Japan', ' USA', ' UK', ' Australia', ' Canada']
Splitting String 2 from Right =  ['Find', 'Free', 'tutorials', 'on', 'Python', 'Programming']
Splitting String 3 from Right =  ['123 ', ' abc ', ' 456 ', ' [email protected]']
After Splitting String 7 =  ['Tutorial', 'Gateway', 'Team']

First, we declared three String variable Str1, Str2, Str3, and assigned corresponding value using the following statement.

Str1 = 'India, China, Japan, USA, UK, Australia, Canada'
Str2 = '[email protected]@[email protected]@[email protected]'
Str3 = '123 + abc + 456 + [email protected]'

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

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

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

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

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

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

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

Str7 = 'Tutorial Gateway Team'

Str8 = Str7.rsplit()
print("After Splitting String 7 = ", Str8)

From the Output, you can observe that it is splitting the string base on empty space because that is the default argument of string rsplit function. 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 argument to the rsplit function.

# Python RSplit String Method Example

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

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

# String RSplit function 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 Python RSplit function directly
Str6 = 'https://www.tutorialgateway.org/python/'.rsplit('/', 2)
print("After Splitting String 6 = ", Str6)
Python RSplit String Example 2

The following statement split the Str1 string based on the separator we specified (i.e., ‘,’) and prints the output. Here, the second argument restricts the rsplit function to split one word only.

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

The second argument restricts the split function to split five words only.

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

We used the Python String rsplit directly. The following statement split the string based on the ‘/’ symbol and prints the output.

Str6 = 'https://www.tutorialgateway.org/python/'.rsplit('/', 2)
print("After Splitting String 6 = ", Str6)