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:
Converting String to Stream of chars
Python Numbers, Type Conversion and Mathematics
Python datetime
Python Set isdisjoint()
Python next()
Python sum()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Find Armstrong Number in an Interval
Python Program to Find ASCII Value of Character
Python Program to Find the Factorial of a Number
Python any()
Python Program to Get Line Count of a File
Python Program to Split a List Into Evenly Sized Chunks
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Find the Largest Among Three Numbers
Python Errors and Built-in Exceptions
Python Set add()
Python Dictionary pop()
Python Program to Copy a File
Python hasattr()
Python String casefold()
Python Program to Solve Quadratic Equation
Python pass statement
Python list()
Python Program to Count the Number of Digits Present In a Number
Python bytes()
Python frozenset()
Python Program to Get File Creation and Modification Date
Python Program to Check the File Size
Python Dictionary
Python int()
Check if a String is a Palindrome in Java