Python: Can't find the value of index of a substring in a string

Thread Starter

zulfi100

Joined Jun 7, 2012
656
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.

Code:
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:


Zulfi.
 

WBahn

Joined Mar 31, 2012
26,398
Look at what

string1.find(string2)

does.

Not what you think it does. Not what you would like it to do. Look up what it actually does.
 

djsfantasi

Joined Apr 11, 2010
7,754
Look at what

string1.find(string2)

does.

Not what you think it does. Not what you would like it to do. Look up what it actually does.
He is programming in Python. The .find method in Python acts differently than C++ or JavaScript. In fact it acts exactly like he would like it to do
 

djsfantasi

Joined Apr 11, 2010
7,754
The Python keyword “in” acts more like .find in the other languages. This is the source of his problem. He is trying to conflate a string with a list. THAT is where he’s expecting something that is what he would like versus what it does.
 

WBahn

Joined Mar 31, 2012
26,398
He is programming in Python. The .find method in Python acts differently than C++ or JavaScript. In fact it acts exactly like he would like it to do
I don't know how it works in either Python or C++, but are you claiming that, Python,

string1.find(string2)

finds the index of where the first occurrence of string1 appears in string2?

That's certainly has not been my experience.

So let's see

If I want to find where the substring "Fred" is in the string "Invite Fred to the meeting.", I claim that I do NOT go

index = "Fred".find("Invite Fred to the meeting.")

which is what he is doing and which is what you are saying it does.

I would, instead, think that I should do

index = "Invite Fred to the meeting.".find("Fred")

So let's see how these behave:

Python:
index = "Fred".find("Invite Fred to the meeting.")
print(index)

index = "Invite Fred to the meeting.".find("Fred")
print(index)
The output is
-1
7
 

WBahn

Joined Mar 31, 2012
26,398
The Python keyword “in” acts more like .find in the other languages. This is the source of his problem. He is trying to conflate a string with a list. THAT is where he’s expecting something that is what he would like versus what it does.
He is not having any problem with the 'in'. He is asking if the string "Your Email address" appears within the string "the following information: Your Email address." and, since it does, he is printing out "Yes". No problem.

His problem is that he is trying to find the index of where the string "Your Email address" appears within the string "the following information: Your Email address." and is, instead, asking Python to find where the string "the following information: Your Email address." appears within the string "Your Email address" but, since it doesn't appear at all, he get's -1 back.

That is a problem with how he is expecting the find() method to work. Not with how he is expecting the 'in' operator to work.



"
 

djsfantasi

Joined Apr 11, 2010
7,754
He is not having any problem with the 'in'. He is asking if the string "Your Email address" appears within the string "the following information: Your Email address." and, since it does, he is printing out "Yes". No problem.

His problem is that he is trying to find the index of where the string "Your Email address" appears within the string "the following information: Your Email address." and is, instead, asking Python to find where the string "the following information: Your Email address." appears within the string "Your Email address" but, since it doesn't appear at all, he get's -1 back.

That is a problem with how he is expecting the find() method to work. Not with how he is expecting the 'in' operator to work.



"
D’oh! I hate it when I miss the obvious.
 

WBahn

Joined Mar 31, 2012
26,398
Hi,
If you know what it does why don't you post it. This is too slow.
https://www.geeksforgeeks.org/python-string-find/

Zulfi.
Because that doesn't move the bar. You have a long history of wanting people to spoon feed you answers to question that you can easily look up yourself. Your FIRST approach needs to be to look for the answer yourself. Your SECOND is to expect others to help you help yourself in figuring out the answer. WAY down the list is expecting people to just serve up the answer on a silver plate for you.
 
Top