Skip to content
Related Articles

Related Articles

Python String index() Method

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 18 Aug, 2022
View Discussion
Improve Article
Save Article

Python String index() Method allows a user to find the index of the first occurrence of an existing substring inside a given string.

Python String index() Method Syntax:

Syntax:  string_obj.index(substring, begp, endp)

Parameters: 

  • substring : The string to be searched for.
  • begp (default : 0) : This function specifies the position from where search has to be started. 
  • endp (default : length of string) : This function specifies the position from where search has to end.

Return:  Returns the first position of the substring found.

Exception:  Raises ValueError if argument string is not found or index is out of range.

Python String index() Method Example:

Python3




string = 'random'
print("index of 'and' in string:", string.index('and'))


Output:

index of 'and' in string: 1

Example 1: Basic usage of Python String index() Method

Python




# initializing target string
ch = "geeksforgeeks"
 
# initializing argument string
ch1 = "geeks"
 
# using index() to find position of "geeks"
# starting from 2nd index
# prints 8
pos = ch.index(ch1,2)
 
print("The first position of geeks after 2nd index : ",end="")
print(pos)


Output: 

The first position of geeks after 2nd index : 8

Note: The index() method is similar to find(). The only difference is find() returns -1 if the searched string is not found and index() throws an exception in this case.

Example 2: Python String index() With start and end Arguments

Python3




test_string = "1234gfg4321"
# finding gfg in string segment 'gfg4'
print(test_string.index('gfg', 4, 8))
 
# finding "21" in string segment 'gfg4321'
print(test_string.index("21", 8, len(test_string)))
 
# finding "32" in string segment 'fg432' using negative index
print(test_string.index("32", 5, -1))


Output:

4
9
8

Exception when using Python String index() Method

ValueError: This error is raised in the case when the argument string is not found in the target string.

Python




# initializing target string
ch = "geeksforgeeks"
 
# initializing argument string
ch1 = "gfg"
 
# using index() to find position of "gfg"
# raises error
pos = ch.index(ch1)
 
print("The first position of gfg is : ",end="")
print(pos)


Output:

Traceback (most recent call last):
  File "/home/aa5904420c1c3aa072ede56ead7e26ab.py", line 12, in 
    pos = ch.index(ch1)
ValueError: substring not found

Practical Application

Python String index() Method function is used to extract the suffix or prefix length after or before the target word. The example below displays the total bit length of an instruction coming from AC voltage given information in a string.

Python




# initializing target strings
VOLTAGES = ["001101 AC", "0011100 DC", "0011100 AC", "001 DC"]
 
# initializing argument string
TYPE = "AC"
 
# initializing bit-length calculator
SUM_BITS = 0
 
for i in VOLTAGES:
 
    ch = i
 
    if ch[len(ch) - 2] != "D":
        # extracts the length of bits in string
        bit_len = ch.index(TYPE) - 1
 
        # adds to total
        SUM_BITS = SUM_BITS + bit_len
 
print("The total bit length of AC is : ", SUM_BITS)


Output: 

The total bit length of AC is : 13

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!