-5

I want to know, is there a way to check if a string is present in the list of strings ;

I want some thing like :

a = ["one", "two", "three", "four"]
b = "three"
if b in a:
    # return index of a
else:
    # do something else
Namit Singal
  • 1,506
  • 12
  • 26
Manipal King
  • 422
  • 1
  • 5
  • 18

2 Answers2

2

If all you want to do is return the index of b inside of a, if b is present, and so something else if it is not, try this : -

a = ["one", "two", "three", "four"]
b = "three"
try:
    print a.index(b) #if inside the function, use return
except:
    #do something2
Namit Singal
  • 1,506
  • 12
  • 26
0

As other experts said, I would highly recommend you try it with your own Python 2.x/3.x environment.

If you for some reason don't have it in hand, try some online IDEs, for example https://ideone.com/

NonStatic
  • 951
  • 1
  • 8
  • 27