Python string Method - rsplit()
The rsplit()
method is a built-in string method in Python that splits a string into a list of substrings based on a specified separator, starting from the right side of the string.
The syntax for using rsplit()
method is as follows:
string.rsplit(separator, maxsplit)
Here, string
is the original string, separator
is the substring to use as the separator (optional), and maxsplit
is the maximum number of splits to perform (optional). If separator
is not specified, the method uses any whitespace character as the separator. If maxsplit
is not specified, the method performs as many splits as possible.
The method returns a list containing the resulting substrings.
Here's an example of using the rsplit()
method:
string = "Hello, World!" substrings = string.rsplit(",") print(substrings)
Output:
['Hello', ' World!']
In the example above, the rsplit()
method was used to split the original string "Hello, World!"
based on the comma separator (","
) starting from the right side of the string. The resulting list is stored in the variable substrings
, which contains the two substrings: "Hello"
and " World!"
.