Python Program to input any sentence and sub-string print the index of given sub-string if found else print -1

 

mainstring=input('Enter any sentence : ')

tofind=input('Enter any word to find : ')

l1=len(tofind)

temp=False

for i in range(len(mainstring)-l1+1):

    twochar=mainstring[i:i+l1]

    if twochar==tofind:

       #print('Found at index : ',i)

       temp=True

    break

if temp==True:

    print('Found at index : ',i)

else:

    print('Not found at ',-1)





Comments

Some popular posts

Python Program that computes the net amount of a bank account based a transaction log from console input.