Table of Contents
The rsplit() method splits string from the right at the specified separator and returns a list of strings.
The syntax of rsplit()
is:
str.rsplit([separator [, maxsplit]])
1. rsplit() Parameters
rsplit()
method takes maximum of 2 parameters:
- separator (optional)- The is a delimiter.
rsplit()
method splits string starting from the right at the specified separator.
If the separator is not specified, any whitespace (space, newline etc.) string is a separator. - maxsplit (optional) – The maxsplit defines the maximum number of splits.
The default value of maxsplit is -1, meaning, no limit on the number of splits.
2. Return Value from rsplit()
rsplit()
breaks the string at the separator starting from the right and returns a list of strings.
3. Example 1: How rsplit() works in Python?
text= 'Love thy neighbor' # splits at space print(text.rsplit()) grocery = 'Milk, Chicken, Bread' # splits at ',' print(grocery.rsplit(', ')) # Splitting at ':' print(grocery.rsplit(':'))
Output
['Love', 'thy', 'neighbor'] ['Milk', 'Chicken', 'Bread'] ['Milk, Chicken, Bread']
When maxsplit is not specified, rsplit()
behaves like split()
.
4. Example 2: How split() works when maxsplit is specified?
grocery = 'Milk, Chicken, Bread, Butter' # maxsplit: 2 print(grocery.rsplit(', ', 2)) # maxsplit: 1 print(grocery.rsplit(', ', 1)) # maxsplit: 5 print(grocery.rsplit(', ', 5)) # maxsplit: 0 print(grocery.rsplit(', ', 0))
Output
['Milk, Chicken', 'Bread', 'Butter'] ['Milk, Chicken, Bread', 'Butter'] ['Milk', 'Chicken', 'Bread', 'Butter'] ['Milk, Chicken, Bread, Butter']
If maxsplit is specified, the list will have the maximum of maxsplit+1
items.
Related posts:
Python Set copy()
Python Dictionary copy()
Python break and continue
Python List count()
Python Program to Find Hash of File
Python String splitlines()
Intelligent Projects Using Python - Santanu Pattanayak
Python Program to Generate a Random Number
Python getattr()
Python Dictionary get()
Python Machine Learning - Sebastian Raschka
Python Custom Exceptions
Python Program to Iterate Through Two Lists in Parallel
Python String strip()
Python String istitle()
Python input()
Deep Learning with Python - Francois Chollet
Python Dictionary pop()
Python Program to Count the Number of Digits Present In a Number
Python Program to Get the Full Path of the Current Working Directory
Python chr()
Python Program to Create Pyramid Patterns
Python Program to Remove Duplicate Element From a List
Python Errors and Built-in Exceptions
Python Program to Find Numbers Divisible by Another Number
Python Deep Learning Cookbook - Indra den Bakker
Python String lower()
Java InputStream to String
Python Program to Find Armstrong Number in an Interval
String Hashing
Python Dictionary clear()
Python Program to Iterate Over Dictionaries Using for Loop