Python String rsplit() Method

Splits the string by the given separator, from right to left. An array is returned for the separated strings. And optional number of occurrences can be specified.

Syntax

Python
string.rsplit(separator = " ", max_strings = -1)

Parameters

ParameterDescription
separator Optional. The string by which to split the given string. The default is a space (" ")
max_strings Optional. The maximum number of strings to return. The default is -1 which means "all strings that resulted from the split operation".

Example

Python
str = 'You say tomato, I say tomato, the French say tomate.'
print(str.rsplit('tomato'))
print(str.rsplit('tomato', 1))

Output

['You say ', ', I say ', ', the French say tomate.']
['You say tomato, I say ', ', the French say tomate.']

Important Note

If you specify max_strings then the number of items that will be returned is max_strings+1.