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 lines after splitting the given line on the right side with the specified delimiter.
Error:
We will not get any error even if we are not passing any argument.
CODE # 1
# Python code to split a line # using rsplit. # Split into space word = ’geeks for geeks ’ print (word.rsplit ()) # Split into “g”. Please note that we have # there is a maximum limit of 1. So # on the right there is one splitting # and we get "x" and & quot; geeks, for & quot; word = ’ geeks, for, geeks’ print (word.rsplit ( ’g’ , 1 ) ) # Splitting into & # 39; @ & # 39; 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 ( ’,’ cod e> , 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 the separator is “No” # so it will be split into 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’]
Python String | rsplit () Python functions: Questions
Python String | rsplit () split: Questions