Python string rpartition()
This tutorial will give us some knowledge about the use of the rpatition() function in Python programming language.
string rpartition() in Python
The rpartition() function is using in Python for splitting the given string by using a separator.
That separator is present in that string only, And return the result as a tuple.
The returned tuple consists of left one to the separator as one string,
the middle one is itself(separator) as another string,
And finally, the right one to the separator as another string,
And all strings are separated by ‘,’ in a tuple.
Syntax of rpartition()
- Here separator is must be at least one but not null and must be present in the given string.
- And one main thing is that the searching will take a charge from the right side to the left side of a string.
- Once the separator is found splitting will be done at that place by using the separator.
str1='College is remains closed on Sunday and on Monday' str2='she is very innocent and she is good human being' print(str1.rpartition('closed')) print(str1.rpartition('c')) print(str2.rpartition('is')) print(str2.rpartition())
('College is remains ', 'closed', ' on Sunday and on Monday') ('College is remains ', 'c', 'losed on Sunday and on Monday') ('she is very innocent and she ', 'is', ' good human being') Traceback (most recent call last): File "C:/Users/gnr90/OneDrive/Desktop/python/rpartition.py", line 6, in <module> print(str2.rpartition()) TypeError: rpartition() takes exactly one argument (0 given)
From the above example, we observed that when we take ‘closed’ in str1 as a separator the string is separated,
And we got the result as a tuple with three strings that are present in the given string.
And when we take ‘is’ as a separator in str2 there are two ‘is’ strings are there so the splitting is done at first ‘is’ from the right side.
when we are not giving any separator the result came as a type error, also we should not give separator as ‘ ‘ (empty).
ALso read: Python Global interpreter lock