TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | rindex() method with Examples

Python String rindex() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String rindex() Method

Python 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.

Signature

rindex(sub[, start[, end]])

Parameters

sub : substring to be searched.

start (optional) : starting index to start searching.

end (optional) : end index where to search stopped.

Return

It 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 Example

A 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 2

This 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 3

If 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




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf