C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String rsplit() MethodPython rsplit() method seperates the string and returns a list. It splits from the right using seperator as a delimiter. If seperator is not specified, any whitespace string is a separator. This method does same as split() except splitting from the right which is described in detail below. Note: if separator is not given, whitespace is treated as separator.Signaturersplit(sep=None,maxsplit=-1) Parameterssep: A string parameter acts as a seperator. maxsplit: number of times split perfomed. ReturnIt returns a comma separated list. Let's see some examples of rsplit() method to understand it's functionality. Python String rsplit() Method Example 1This is a simple example to understand the usage of rsplit() method. # Python rsplit() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.rsplit() # Displaying result print(str2) Output: ['Java', 'is', 'a', 'programming', 'language'] Python String rsplit() Method Example 2Let's pass a parameter separator to the method, see the example. # Python rsplit() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.rsplit('Java') # Displaying result print(str2) Output: ['', ' is a programming language'] Python String rsplit() Method Example 3The string is splited each time when a is occurred. See the example below. # Python rsplit() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.rsplit('a') # Displaying result print(str2) Output: ['J', 'v', ' is ', ' progr', 'mming l', 'ngu', 'ge'] Python String rsplit() Method Example 4Along with separator, we can also pass maxsplit value. The maxsplit is used to set the number of times to split. # Python rsplit() method example # Variable declaration str = "Java is a programming language" # Calling function str2 = str.rsplit('a',1) # Displaying result print(str2) str2 = str.rsplit('a',3) # Displaying result print(str2) Output: ['Java is a programming langu', 'ge'] ['Java is a progr', 'mming l', 'ngu', 'ge']
Next TopicPython Strings
|