Python String rsplit() method
Python String rsplit() method
The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the same output as the string.split() method.
Syntax
string.rsplit([sep [,maxsplit]])
Parameter
sep (optional): This parameter specifies the separator to use when splitting the string. The default value is whitespace.
maxsplit (optional): This parameter Specifies how many splits to do. The default value is -1, which is "all occurrences".
Return
This method returns a list of the words in the string, using sep as the delimiter string.
Example 1
# Python program explaining # the string.rsplit() method # initializing the string string = "Python Java C# Ruby" print("Original string...") print(string) # spliting the string when whitespaces ocures print("After spliting the string …") print(string.rsplit())
Output
Original string... Python Java C# Ruby After spliting the string... ['Python', 'Java', 'C#', 'Ruby']
Example 2
# Python program explaining # the string.rsplit() method string = "Tutorials and Examples" # By default this method Splits at space print(string.rsplit()) # Splits at 'g'. As the maximum limit as 1. So # from right, the splitting occurs at second t string = "Tutorials, and, Examples" print(string.rsplit('t', 1)) # Splitting at '$' with maximum splitting # as 1 string = "Tutorials$and$Examples" print(string.rsplit('$', 1))
Output
['Tutorials', 'and', 'Examples'] ['Tu', 'orials, and, Examples'] ['Tutorials$and', 'Examples']