C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python String rindex() MethodPython rindex() method works same as the rfind() method except it throws error ValueError. This method throws an exception ValueError if the substring is not found. The syntax is given below. Signaturerindex(sub[, start[, end]]) Parameterssub : substring to be searched. start (optional) : starting index to start searching. end (optional) : end index where to search stopped. ReturnIt returns either index of substring or throws an exception ValueError. Let's see some examples of rindex() method to understand it's functionality. Python String rindex() Method Example : Simple ExampleA simple example is created first to see how to implement this method. This method returns index of the substring. # Python rindex() method example # Variable declaration str = "It is technical tutorial" # calling function str2 = str.rindex("t") # No start and end is given # displaying result print(str2) Output: 18 Python String rindex() Method Example 2This method accept parameters start and end index to search subtring from the substring. See the example below. # Python rindex() method example # Variable declaration str = "It is technical tutorial" # calling function str2 = str.rindex("t") # No start and end is given # displaying result print(str2) str2 = str.rfind("t",5,15) # Start is end both are given print(str2) Output: 18 6 Python String rindex() Method Example 3If the substring is not found in the string, it raises ValueError. See the example below. # Python rindex() method example # Variable declaration str = "Hello C Language" # calling function str2 = str.rindex("t") # No start and end is given # displaying result print(str2) str2 = str.rfind("t",5,15) # Start is end both are given print(str2) Output: ValueError: substring not found
Next TopicPython Strings
|