Jump to content

Python: Finding the index of a substring in a string


Recommended Posts

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.

Link to post
Share on other sites
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")

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.