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:
1 | 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?
1 2 3 4 5 6 7 8 9 10 11 12 | 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
1 2 3 | [ '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?
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
1 2 3 4 | [ '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:
Format ZonedDateTime to String
Python bin()
Python Program to Count the Occurrence of an Item in a List
Array to String Conversions
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Sort Words in Alphabetic Order
Python List sort()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python String rpartition()
Python reversed()
Python Tuple count()
JavaScript Eval: run a code string
Python Program to Split a List Into Evenly Sized Chunks
Convert String to int or Integer in Java
Python Program to Check if a Key is Already Present in a Dictionary
Count Occurrences of a Char in a String
Python Program to Check the File Size
Python tuple()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Set intersection()
Python Program to Create a Countdown Timer
Python Program to Check Whether a String is Palindrome or Not
Python type()
Python Package
Python filter()
Python Sets
Python Matrices and NumPy Arrays
Python String endswith()
Python Program to Check Armstrong Number
Python Program to Count the Number of Digits Present In a Number
Python Program to Find the Factors of a Number
Python Program to Randomly Select an Element From the List