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

#### 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

print("index =", ind)

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,432
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
8,391
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
8,391
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,432
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,432
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.

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
8,391

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,432
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.

#### WBahn

Joined Mar 31, 2012
26,432
D’oh! I hate it when I miss the obvious.
Don't we all! Yet it won't be the last time either you or me does it. Cheers!

#### warnerjonn

Joined Jun 16, 2022
1
When you use string_object.index(substring), it looks for the occurrence of substring in the string_object. If substring is present, the method returns the index at which the substring is present, otherwise, it throws ValueError: substring not found.

Using Python’s “in” operator

The simplest and fastest way to check whether a string contains a substring or not in Python is the “in” operator . This operator returns true if the string contains the characters, otherwise, it returns false .

str="Hello, World!"
print("World" in str)//output is True

Python “in” operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string. It is important to note that the “in” operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.

#### MrSalts

Joined Apr 2, 2020
1,833
When you use string_object.index(substring), it looks for the occurrence of substring in the string_object. If substring is present, the method returns the index at which the substring is present, otherwise, it throws ValueError: substring not found.

Using Python’s “in” operator

The simplest and fastest way to check whether a string contains a substring or not in Python is the “in” operator . This operator returns true if the string contains the characters, otherwise, it returns false .

str="Hello, World!"
print("World" in str)//output is True

Python “in” operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string. It is important to note that the “in” operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.
Yes, that is how the in operator works but the OP asked...
I am trying to find out the index of a substring in a string.
Unfortunately, your result of "true" using the in operator is not an index as the OP was seeking. Also, the thread is almost two years old. I'm hoping the OP has not paused for the past two years waiting for a second solution to his question.

welcome to the forum.

#### Ya’akov

Joined Jan 27, 2019
6,010
When you use string_object.index(substring), it looks for the occurrence of substring in the string_object. If substring is present, the method returns the index at which the substring is present, otherwise, it throws ValueError: substring not found.

Using Python’s “in” operator

The simplest and fastest way to check whether a string contains a substring or not in Python is the “in” operator . This operator returns true if the string contains the characters, otherwise, it returns false .

str="Hello, World!"
print("World" in str)//output is True

Python “in” operator takes two arguments, one on the left and one on the right, and returns True if the left argument string is contained within the right argument string. It is important to note that the “in” operator is case sensitive i.e, it will treat the Uppercase characters and Lowercase characters differently.
Welcome to AAC.

The impulse to be helpful is greatly appreciated. This community is based on mutual help and all are welcome to participate. It is important to check the date of a thread before posting in it. Reviving old threads should be done with care, only if it has a benefit which overcomes the side effect of a renewed discussion that doesn’t help anyone.

Another useful thing is to check the TS (Thread Starter, AAC-speak for “OP”) member’s last seen date. You can find it by clicking on the user name at the top of the post.

In any case, this is just some housekeeping stuff. It’s great to see you have joined us, please don’t stop trying to help, it’s greatly appreciated.