user2005803
user2005803

Reputation: 73

string.find not working with list

Seeing a weird issue with string.find.

I have a following list:

lstofpro = ["Brown, John", "Smith,Jon"]
keywordstring = "Something: Smith,Jon Account Number: 99999"

for p in lstofpro:
    if keywordstring.find(p.strip()) != -1:
        print ("Found a match for : %s" % p)

The above doesn't find a successful match even if the value exists in the keyworstring. If I change p.strip() to hard coded value of "Smith,Jon" it successfully finds it.

Guys have any clue what could be wrong?

Upvotes: 0

Views: 2360

Answers (1)

SuperFamousGuy
SuperFamousGuy

Reputation: 1575

Is there a reason you can't use the "in" operator? I tried your algorithm with it like this and got the desired results:

lstofpro = ["Brown, John", "Smith,Jon"]
keywordstring = "Something: Smith,Jon Account Number: 99999"

for p in lstofpro:
   if p in keywordstring:
      print ("Found a match for : %s" % p)

Upvotes: 1

Related Questions