Python String rsplit() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

Python String rsplit() method

The rsplit() method in Python splits a string into a list, starting from the right.

If no maxsplit parameter is specified, this method will return the same as the split() method.

When maxsplit is specified, the list will contain the specified number of elements plus one.

Syntax of rsplit() method
string.rsplit(separator, maxsplit)

separator: Specifies the separator to 
           use when splitting the string(Optional). 
           By default any whitespace is a separator
maxsplit:  Specifies how many splits to do(Optional). 
           Default value is -1, which is "all occurrences"
Example

Code/Output

str1 = "Pylenin loves Python"
print(str1.rsplit())
>>> ['Pylenin', 'loves', 'Python']

# Using the maxsplit parameter
str1 = "Pylenin loves Python"
print(str1.rsplit(" ", 1))
>>> ['Pylenin loves', 'Python']

str1 = "Pylenin loves Python"
print(str1.rsplit("loves", 1))
>>> ['Pylenin ', ' Python']

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide