Python String rsplit() Method Examples

In the previous article, we have discussed Python String rpartition() Method Examples
rsplit() Method in Python:

Starting from the right, the rsplit() method splits a string into a list.

If no “max” parameter is specified, this method will return the same value as split().

Note: When maxsplit is specified, the list will have the number of elements specified plus one.

Syntax:

string.rsplit(separator, maxsplit)

Parameters

separator: This is Optional. This parameter specifies the separator to be used when splitting the string. By default, any whitespace serves as a separator.

maxsplit: This is Optional. Specifies the number of splits to perform. The default value is -1, which represents “all occurrences.”

Examples:

Example1:

Input:

Given string = "welcome,to,python,programs"
Given separator = ","
Given maxsplit value = 1

Output:

The above given string is : welcome,to,python,programs
The given string after applying rsplit() function:
['welcome,to,python', 'programs']

Example2:

Input:

Given string = "good morning this is btechgeeks"

Output:

The above given string is : good morning this is btechgeeks
The given string after applying rsplit() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

Note:

If you don't give any separator and maxsplit values it takes space,-1 values
respectively by default.

String rsplit() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the separator as static input and store it in another variable.
  • Give the maxsplit as static input and store it in another variable.
  • Apply rsplit() method to the given string for the given separator and maxsplit() values which splits a string into a list starting from the right.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying rsplit() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • The Exit of Program.

Below is the implementation: 

# Give the first string as static input and store it in a variable.
gvn_fststr = "welcome,to,python,programs"
# Give the separator as static input and store it in another variable.
gvn_separtr = ","
# Give the maxsplit as static input and store it in another variable.
maxsplt_valu = 1
# Apply rsplit() method to the given string for the given separator and
# maxsplit() which splits a string into a list starting from the right.
# Store it in another variable.
rslt_str1 = gvn_fststr.rsplit(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rsplit() function.
print("The given first string after applying rsplit() function:")
print(rslt_str1)
# Similarly do the same for other string without giving the separator and maxsplit values
# and print the result list obtained.
gvn_scndstr = "good morning this is btechgeeks"
rslt_str2 = gvn_scndstr.rsplit()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rsplit() function:")
print(rslt_str2)

 Output:

The above given first string is : welcome,to,python,programs
The given first string after applying rsplit() function:
['welcome,to,python', 'programs']
The above given second string is : good morning this is btechgeeks
The given second string after applying rsplit() function:
['good', 'morning', 'this', 'is', 'btechgeeks']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the separator as user input using the input() function and store it in another variable.
  • Give the maxsplit as user input using the int(input()) function and store it in another variable.
  • Apply rsplit() method to the given string for the given separator and maxsplit() values which splits a string into a list starting from the right.
  • Store it in another variable.
  • Print the above-given string.
  • Print the above-given string after applying rsplit() function.
  • Similarly, do the same for the other string without giving the separator and maxsplit values and print the result list obtained.
  • The Exit of Program.

Below is the implementation:

# Give the first string as user input using the input() function and store it in a variable.
gvn_fststr = input("Enter some random string = ")
# Give the separator as user input using the input() function and store it in another variable.
gvn_separtr = input("Enter some random separator = ")
# Give the maxsplit as user input using the int(input()) function and store it in another variable. 
maxsplt_valu = int(input("Enter some random number = "))
# Apply rsplit() method to the given string for the given separator and
# maxsplit() which splits a string into a list starting from the right.
# Store it in another variable.
rslt_str1 = gvn_fststr.rsplit(gvn_separtr, maxsplt_valu)
# Print the above given string
print("The above given first string is :", gvn_fststr)
# Print the above given string after applying rsplit() function.
print("The given first string after applying rsplit() function:")
print(rslt_str1)
# Similarly do the same for other string without giving the separator and maxsplit values
# and print the result list obtained.
gvn_scndstr = input("Enter some random string = ")
rslt_str2 = gvn_scndstr.rsplit()
print("The above given second string is :", gvn_scndstr)
print("The given second string after applying rsplit() function:")
print(rslt_str2)

Output:

Enter some random string = hello this is btechgeeks
Enter some random separator = 
Enter some random number = 1
The above given first string is : hello this is btechgeeks
The given first string after applying rsplit() function:
['hello this is', 'btechgeeks']
Enter some random string = hello all goodmorning####
The above given second string is : hello all goodmorning####
The given second string after applying rsplit() function:
['hello', 'all', 'goodmorning####']

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.