Python String index() and its applications

Finding the string(substring) in a string is an application that has many uses in day-to-day life. Python offers this using a function index(), which returns the position of the first occurrence of substring in a string

Syntax : 
ch.index(ch1,begp,endp)
Parameters : 
ch1 : The string to be searched for.
begp (default : 0) :  This function specifies the position
from where search has to be started.
endp (default : string_len) :  This function specifies the 
position from where search has to end.
Return Value : 
Returns the first position of the substring found.
Exception : 
Raises ValueError if argument string is not found.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the working of 
# index()
  
# 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)

chevron_right


Output :

The first position of geeks after 2nd index : 8

Exceptions

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the exception of 
# index()
  
# 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)

chevron_right


Runtime Error :

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

Practical Application : This 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 instruction coming from AC voltage given information in string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate the application of 
# index()
  
# 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 : ",end="")
print (sum_bits)

chevron_right


Output :

The total bit length of AC is : 13



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.