Python String rsplit() Method

The Python rsplit() method is a string method that splits a string into a list of substrings, starting from the right end of the string and using a specified separator as the delimiter. It has the following syntax:

string.rsplit(separator, maxsplit)

where separator is an optional parameter that specifies the string to split on (default is any whitespace), maxsplit is an optional parameter that specifies how many splits to do from the right (default is -1, which means all possible splits).

The rsplit() method returns a list of substrings after splitting the string.

Here are some examples of using the Python rsplit() method:

# Example 1: Split a string by comma
mystr = 'apple,banana,cherry'
result = mystr.rsplit(',')
print(result)

# Output: ['apple', 'banana', 'cherry']

# Example 2: Split a string by space with maxsplit=2
mystr = 'This is a sample sentence'
result = mystr.rsplit(' ', 2)
print(result)

# Output: ['This is a', 'sample', 'sentence']

# Example 3: Split a string by any whitespace
mystr = 'Hello\tWorld\nPython'
result = mystr.rsplit()
print(result)

# Output: ['Hello', 'World', 'Python']

If you have any questions about this code, you can drop a line in comment.

Comments

Popular posts from this blog

Python chr() Built in Function

Stock Market Predictions with LSTM in Python

Collections In Python

Python Count Occurrence Of Elements

Python One Liner Functions