Jump to content

Python: Finding the index of a substring in a string

Featured Replies

Hi,

I am trying to find out the index of a substring in a string. "in" tells that the string is present but "find" is returning -1.

import numpy as np
cnt = 0
response = "the following information: Your Email address."
ind = "Your Email address".find(response)
print("index =", ind)
if "Your Email address" in response:
    print("Yes")

Somebody please guide me how can I get the index of substring in the string.

Following is the output:

Quote

index = -1
Yes

Zulfi.

  • Author

Hi,

 

Thanks. The correct concept is:

ind = response.find("Your Email address)

 

1 hour ago, zak100 said:

The correct concept is:


ind = response.find("Your Email address)

 

That seems pretty close. Add the missing " after the string and the code will run:  

Quote

ind = response.find("Your Email address")

 

  • 1 year later...

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.