rsplit() returns a list after breaking a string using delimiter and max value
my_str.rsplit(delimiter,max_value)
delimiter
: to be used to break the string
max_value
: optional , the number of elements in output plus one. Default value is -1 so it includes all occurance.
my_list="'Alex','Ronald','John'"
my_list=my_list.rsplit(',')
print(my_list)
output
["'Alex'", "'Ronald'", "'John'"]
Without delimiter
my_list="'Alex','Ronald','John'"
my_list=my_list.rsplit()
print(my_list)
["'Alex','Ronald','John'"]
If delimiter is not found
my_list="'Alex','Ronald','John'"
my_list=my_list.rsplit('*')
print(my_list)
Output
["'Alex','Ronald','John'"]
Using one email address to separate domain and userid part
my_list="userid@example.com"
my_list=my_list.rsplit('@')
print(my_list)
Output
['userid', 'example.com']
difference between rsplit() and split()
We can use optional value max_value to specify number of splits. After reaching this max_value rest of the string is returned as it is without any further break.
See the difference in output below.
my_str='Welcome to plus2net'
print(my_str.rsplit('e')) # without any max_value
print(my_str.rsplit('e',2))
print(my_str.split('e',2))
Output
['W', 'lcom', ' to plus2n', 't']
['Welcom', ' to plus2n', 't']
['W', 'lcom', ' to plus2net']
« All String methods
← Subscribe to our YouTube Channel here