Python String | rsplit()
rsplit() method returns a list of strings after breaking the given string from right side by the specified separator.
Syntax :
str.rsplit(separator, maxsplit)
Parameters :
separator : The is a delimiter. The string splits at this specified separator starting from right side. If is not provided then any white space is a separator.
maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then there is no limit.
Return :
Returns a list of strings after breaking the given string from right side by the specified separator.
Error :
We will not get any error even if we are not passing any argument.
CODE #1
# Python code to split a string # using rsplit. # Splits at space word = 'geeks for geeks'print(word.rsplit()) # Splits at 'g'. Note that we have # provided maximum limit as 1. So # from right, one splitting happens # and we get "eeks" and "geeks, for, " word = 'geeks, for, geeks'print(word.rsplit('g', 1)) # Splitting at '@' with maximum splitting # as 1 word = 'geeks@for@geeks'print(word.rsplit('@', 1)) |
Output :
['geeks', 'for', 'geeks'] ['geeks, for, ', 'eeks'] ['geeks@for', 'geeks']
CODE #2
word = 'geeks, for, geeks, pawan' # maxsplit: 0 print(word.rsplit(', ', 0)) # maxsplit: 4 print(word.rsplit(', ', 4)) word = 'geeks@for@geeks@for@geeks' # maxsplit: 1 print(word.rsplit('@', 1)) # maxsplit: 2 print(word.rsplit('@', 2)) |
Output :
['geeks, for, geeks, pawan'] ['geeks', 'for', 'geeks', 'pawan'] ['geeks@for@geeks@for', 'geeks'] ['geeks@for@geeks', 'for', 'geeks']
CODE #3
word = 'geeks for geeks' # Since seperator is 'None', # so, will be splitted at space print(word.rsplit(None, 1)) print(word.rsplit(None, 2)) # Also observe these print('@@@@@geeks@for@geeks'.rsplit('@')) print('@@@@@geeks@for@geeks'.rsplit('@', 1)) print('@@@@@geeks@for@geeks'.rsplit('@', 3)) print('@@@@@geeks@for@geeks'.rsplit('@', 5)) |
Output :
['geeks for', 'geeks'] ['geeks', 'for', 'geeks'] ['', '', '', '', '', 'geeks', 'for', 'geeks'] ['@@@@@geeks@for', 'geeks'] ['@@@@', 'geeks', 'for', 'geeks'] ['@@', '', '', 'geeks', 'for', 'geeks']
Recommended Posts:
- numpy string operations | rsplit() function
- Python | Pandas Reverse split strings into two List/Columns using str.rsplit()
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if string ends with any string in given list
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python String
- Python String | min()
- Python String | max()
- Python | Add one string to another
- Python String | zfill()
- Python String | format_map()
- Python String | split()
- Python String | splitlines()
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




