string.rpartition(separator)
- separator: The substring by which the string is split from the right.
Three parts are here.
my_str='Welcome to Plus2net'
output=my_str.rpartition('to')
print(output)
Output is here
('Welcome ', 'to', ' Plus2net')
We can add one more line to display the last element
print(output[2])
Output
plus2net
If matching string is not found then rpartition() returns the tuple with 3rd element as full string.
my_str='Welcome to Plus2net'
output=my_str.rpartition('ab')
print(output)
Output is here
('', '', 'Welcome to Plus2net')
If we use partition() in place or rpartition() the output is here ( the difference between rpartition() and partition() )
('Welcome to Plus2net', '', '')
text = "apple-orange-banana"
result = text.rpartition("-")
print(result)
Output:
('apple-orange', '-', 'banana')
text = "apple-orange-banana"
result = text.rpartition("/")
print(result)
Output:
('', '', 'apple-orange-banana')
filename = "document.pdf"
name, sep, extension = filename.rpartition(".")
print("Name:", name)
print("Extension:", extension)
Output:
Name: document
Extension: pdf
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.