Table of Contents
In this tutorial, we will learn about the Python index() method with the help of examples.
The index()
method returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception.
Example
text = 'Python is fun' # find the index of is result = text.index('is') print(result) # Output: 7
1. index() Syntax
It’s syntax is:
str.index(sub[, start[, end]] )
2. index() Parameters
The index()
method takes three parameters:
- sub – substring to be searched in the string str.
- start and end(optional) – substring is searched within str[start:end]
3. index() Return Value
- If substring exists inside the string, it returns the lowest index in the string where substring is found.
- If substring doesn’t exist inside the string, it raises a ValueError exception.
The index()
method is similar to the find() method for strings.
The only difference is that find() method returns -1 if the substring is not found, whereas index()
throws an exception.
4. Example 1: index() With Substring argument Only
sentence = 'Python programming is fun.' result = sentence.index('is fun') print("Substring 'is fun':", result) result = sentence.index('Java') print("Substring 'Java':", result)
Output
Substring 'is fun': 19 Traceback (most recent call last): File "<string>", line 6, in result = sentence.index('Java') ValueError: substring not found
Note: Index in Python starts from 0 and not 1. So the occurrence is 19 and not 20.
5. Example 2: index() With start and end Arguments
sentence = 'Python programming is fun.' # Substring is searched in 'gramming is fun.' print(sentence.index('ing', 10)) # Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming' print(sentence.index('fun', 7, 18))
Output
15 17 Traceback (most recent call last): File "<string>", line 10, in print(quote.index('fun', 7, 18)) ValueError: substring not found
Related posts:
Python Program to Remove Punctuations From a String
CharSequence vs. String in Java
Python Program to Iterate Through Two Lists in Parallel
Python Program to Find Hash of File
Python min()
Python Dictionary
Python Set symmetric_difference_update()
Python Program to Find Sum of Natural Numbers Using Recursion
Python Object Oriented Programming
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Check If a String Is a Number (Float)
Python hex()
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String isprintable()
Intelligent Projects Using Python - Santanu Pattanayak
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python bytes()
Python for Programmers with introductory AI case studies - Paul Deitel & Harvey Deitel
JavaScript Eval: run a code string
Python Global, Local and Nonlocal variables
Check if a String is a Palindrome in Java
Python Program to Convert String to Datetime
Python Dictionary popitem()
Python String split()
Python Program to Make a Simple Calculator
Python String center()
Python oct()
Python Program to Print Colored Text to the Terminal
Python List Comprehension
Python Program to Randomly Select an Element From the List
Python Set update()
Python Program to Display the multiplication Table