Advertisement



< Prev
Next >



Python - String rsplit() Method





Contrary to the split string method, which started at the left of a string in order to split the invoked string, the rsplit() string method is used to split the content of the invoked string by starting at the right of the string and at the occurrence of a specific separator. The rsplit() method returns a list of split values.

Note : By default, the slit() method splits the content of a string at the occurrence of each whitespace in it but you can specify your own separator as well.




Signature of rsplit() method


count(value, start, end)


Parameters Description
separator
This optional parameter is a string value, which acts a separator, used to split the values of the invoked string. The default separator is whitespace.

maxnumber
This optional parameter is an integer value, used to specify the maximum number of splits to perform. Its default value is -1 i.e. a split for each occurrence of separator value.







When calling the rsplit() method, we can even specify a maximum number(and not more than that) of splits to perform on the invoked string(starting from the right of string), on the basis of a separator. Let us see an example.

# Python - Example of rsplit() method.



# Creating a string 
s1 = 'Eddiee edited it'


# Printing the value of string 
print('String is : ', s1)



# Calling the rsplit() method on string, starting from the right
# which will perform 2 splits on the basis of separator lower case 'e'
a_list = s1.rsplit('e', 2)


# Priting all the spiits 
print('2 Splits when the separator is e : ', a_list)



# Calling the rsplit() method on string, starting from the right
# which will perform 2 splits on the basis of separator lower case 'i'
a_list = s1.rsplit('i', 2)


# Priting all the spiits 
print('2 Splits when the separator is i :', a_list)



# Calling the rsplit() method on string, starting from the right
# which will perform 1 split on the basis of separator lower case 't'
a_list = s1.rsplit('t', 1)


# Priting all the spiits 
print('1 split when the separator is t :', a_list)



# Calling the rsplit() method on string, starting from the right
# which will perform 2 splits on the basis of separator lower case 'd'
a_list = s1.rsplit('d', 2)


# Priting all the spiits 
print('2 Splits when the separator is d :', a_list)


Output is :


String is :  Eddiee edited it
2 Splits when the separator is e :  ['Eddiee ', 'dit', 'd it']
2 Splits when the separator is i : ['Eddiee ed', 'ted ', 't']
1 split when the separator is t : ['Eddiee edited i', '']
2 Splits when the separator is d : ['Eddiee e', 'ite', ' it']





Please share this article -





< Prev
Next >
< String splitl() Method
String splitlines() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement