0

How could I find out what character a variable is in variable. I hope this doesn't sound too confusing.

What I'm trying to do is make a search engine. I have a variable called "currentchar" which is an integar.

So what I'm trying to do is:

if s[i] in l[currentchar:]: #Just checking if a string is in a string...
    currentchar = (Figure out which char "s[i]" started at.)

So for example if s[i] is "ph" and l[currentchar:] is "elephant" I wan't currentchar to be set to 3 because "ph" started 3 characters through "elephant".

I hope people understand what I'm trying to say.

David Callanan
  • 5,601
  • 7
  • 63
  • 105
  • You did not even try to find a solution for yourself, hu? – Mathias Aug 21 '14 at 12:23
  • possible duplicate of [Does Python have a string contains method?](http://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-method) – Mathias Aug 21 '14 at 12:24

1 Answers1

1

Seeing as you are checking that the substring exists, use index

s = "elephant"
print (s.index("ph"))
3

if s[i] in l[currentchar:]: #Just checking if a string is in a string...
    currentchar = l[currentchar:].index(s[i])
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • @user1541397, no worries, if you are using index you want to make sure the substring exists or it will throw an error – Padraic Cunningham Aug 21 '14 at 12:27
  • Alternatively you could use `find`: `currentchar = l[currentchar:].find(s[i])`. Instead of an exception it returns -1 if `find` couldn't find it (so you need an `if` after it not before). Both `str.find` and `str.index` support a start parameter so another way would be to use `currentchar = l.index(s[i], currentchar)`. – xZise Aug 21 '14 at 12:40
  • @xZise, there is nothing wrong with the if check, if the string does not exist I imagine the OP does not want currentchar set to -1 – Padraic Cunningham Aug 21 '14 at 12:43
  • I just wanted to point out that there is also `find`. I also noted that (s)he still needs an if and my second example with the start parameter is actually using `str.index`. – xZise Aug 21 '14 at 13:32