Change language

Python String | rsplit ()

| |

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 ( ’,’ , 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

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method