×

Python String rsplit() method

Python String rsplit() method

The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the same output as the string.split() method.

Syntax

string.rsplit([sep [,maxsplit]])

Parameter

sep (optional): This parameter specifies the separator to use when splitting the string. The default value is whitespace.

maxsplit (optional): This parameter Specifies how many splits to do. The default value is -1, which is "all occurrences".

Return

This method returns a list of the words in the string, using sep as the delimiter string. 

Example 1

 # Python program explaining
 # the string.rsplit() method
 # initializing the string
 string = "Python Java C# Ruby"
 print("Original string...")
 print(string)
 # spliting the string when whitespaces ocures
 print("After spliting the string …")
 print(string.rsplit()) 

Output

 Original string...
 Python Java C# Ruby
 After spliting the string... 
 ['Python', 'Java', 'C#', 'Ruby'] 

Example 2

 # Python program explaining
 # the string.rsplit() method
 string = "Tutorials and Examples"
 # By default this method Splits at space
 print(string.rsplit()) 
 # Splits at 'g'. As the maximum limit as 1. So 
 # from right, the splitting occurs at second t 
 string = "Tutorials, and, Examples"
 print(string.rsplit('t', 1)) 
 # Splitting at '$' with maximum splitting 
 # as 1 
 string = "Tutorials$and$Examples"
 print(string.rsplit('$', 1)) 

Output

 ['Tutorials', 'and', 'Examples']
 ['Tu', 'orials, and, Examples']
 ['Tutorials$and', 'Examples'] 

Related Topics

How to Download Python

How to Download Python Python is the most renowned programming language and developers across the globe are working on projects that are made using Python and its libraries. Here we will...

4 minutes read.

Python list() | List class in Python

The list() class in Python creates a list object where the list object is a collection which is ordered and changeable. Syntax class list([iterable]) Parameter Iterable: It is a required parameter which represents a sequence, collection...

1 minute read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python program to print calendar

Python program to print the calendar This article will explain how to print the calendar of month and year using Python's calendar module. It's a straightforward thing in Python by importing...

1 minute read.

How to assign values to variables in Python and other languages?

Python makes it simple to construct variables. The value to be stored in the variable should then be written after a suitable name for the variable and the equality sign....

3 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Python String join() method

Python String join() method The String.join() method in Python concatenates each element of an iterable (such as list, string and tuple) to the given string and returns the concatenated string. Syntax string.join(seq) Parameter Seq: This...

1 minute read.

Check if the directory exists in Python

To prevent any error, while loading or editing a file, we first have to check that the directory or the file exists or not. This is also done to prevent...

5 minutes read.

Python vs Java

There are a lot of programming languages out there, and every day, a new programming language is developing in some parts of the world. Each programming language is a mixture...

5 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

Python String Variable

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

4 minutes read.

Python Permutations and Combinations

In mathematics, we all studied what is meant by permutations and combinations. “Permutations” define the way of arranging the elements in sequential order. “Combinations” mean the way of selecting the...

7 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Python List index() method

Python List index() method The list.index () method in Python returns the position at the first occurrence of the specified value. Syntax list.index(x[, start[, end]]) Parameter element – This parameter represents the element whose lowest index will be returned. start (Optional)...

2 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.