Python String rpartition() Method

The Python rpartition() method is a string method that splits a string at the last occurrence of a specified separator and returns a tuple containing three elements: the part before the separator, the separator itself, and the part after the separator. It has the following syntax:

string.rpartition(separator)

where separator is a string that specifies where to split the string.

If separator is found in the string, rpartition() returns a tuple with three elements. If separator is not found in the string, rpartition() returns a tuple with two empty strings and the original string.

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

# Example 1: Split a string at 'and'
mystr = 'Do it now and keep it simple'
result = mystr.rpartition('and')

print(result)
# Output: ('Do it now ', 'and', ' keep it simple')

# Example 2: Split a string at '-'
mystr = '2021-03-06'
result = mystr.rpartition('-')
print(result)

# Output: ('2021-03', '-', '06')

# Example 3: Split a string at an unknown separator
mystr = 'I love Python programming'
result = mystr.rpartition('Java')
print(result)

# Output: ('', '', 'I love Python programming')

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