rsplit() method of str class

The rsplit() method of str class makes multiple tokens out of a string using a delimiter-substring and a count.

Str Updated 13 Mar 2017 1 min read

Method Name

rsplit

Method Signature

rsplit(sep=None, maxsplit=-1)

Method Overview

  • The rsplit() method tokenizes a string based on a delimiter-substring and returns a list of tokens.

  • Both the delimiter-substring and the number of splits can be specified using sep, maxsplit parameters.

  • Tokenizing starts at the highest index at which the delimiter-substring specified by the sep is found.

Example

python
csvString   = "t1,t2,t3,t4"

tokens      = csvString.rsplit(",")

print(tokens)

tokens      = csvString.rsplit(",",2)

print(tokens)

##

Output

Output
['t1', 't2', 't3', 't4']

['t1,t2', 't3', 't4']

Example

python
csvString   = "v1||v2||v3||v4"

tokens      = csvString.rsplit("||")

print(tokens)

tokens      = csvString.rsplit("||",2)

print(tokens)

Output

Output
['v1', 'v2', 'v3', 'v4']

['v1||v2', 'v3', 'v4']