zak100 Posted December 3, 2020 Share Posted December 3, 2020 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 comment Share on other sites More sharing options...
Ghideon Posted December 3, 2020 Share Posted December 3, 2020 (edited) 8 minutes ago, zak100 said: Somebody please guide me how can I get the index of substring in the string. https://www.w3schools.com/python/ref_string_find.asp Edited December 3, 2020 by Ghideon 1 Link to comment Share on other sites More sharing options...
zak100 Posted December 5, 2020 Author Share Posted December 5, 2020 Hi, Thanks. The correct concept is: ind = response.find("Your Email address) Link to comment Share on other sites More sharing options...
Ghideon Posted December 5, 2020 Share Posted December 5, 2020 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 Link to comment Share on other sites More sharing options...
Sunila Jha Posted February 2, 2022 Share Posted February 2, 2022 Hi @zak100, I am not sure if you still need help in this or not but if you still does, check this article once - https://www.scaler.com/topics/find-function-in-python/ Hope this will help you some way. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now