2

I would like to check against a list of words if they are within a string.

For Example:

listofwords = ['hi','bye','yes','no']

String = 'Hi how are you'
string2 = 'I have none of the words'

String 1 is true as it contains 'hi' and string2 is false as it does not.

I have tried the following code but it always returns false.

if any(ext in String for ext in listofwords):
   print(String)

I would also like to show what the matching word was to check this is correct.

4
  • 1
    do you want to print the entire string or only the matching words? Commented Jul 30, 2019 at 13:07
  • If your problem not solved please update the question. Commented Jul 30, 2019 at 14:16
  • @Poojan updated Commented Jul 30, 2019 at 14:32
  • Check updated answer. The key idea is to user .lower function on strings before making comparisons. If you want to print words( here assuming words mean space seperated words) just iterate over and do matching. Commented Jul 30, 2019 at 14:51

3 Answers 3

5

hi and Hi are different words. Use .lower before comparing.

if any(ext.lower() in String.lower() for ext in listofwords):
   print(String)

Update:

  • to print matching word use for loop to iterate and print words that match.

Example:

listofwords = ['hi','bye','yes','no']

String = 'Hi how are you'
string2 = 'I have none of the words'

for word in listofwords:
    if word.lower() in map(str.lower,String.split()): # map both of the words to lowercase before matching
        print(word)


for word in listofwords:
    if word.lower() in map(str.lower,string2.split()): # map both of the words to lowercase before matching
        print(word)

PS: Not the optimized version. You can store String.split results in a list and then start iterating that will save time for larger strings. But purpose of the code is to demonstrate use of lower case.

Sign up to request clarification or add additional context in comments.

2 Comments

@AndrejKesely OP stats his problem. Its returning False in every case given the code. I just gave suggestion on mistake. His code do not mention anything about string2
Would this also allow me to return the actual matched value in the string ->ext.lower()
2

Python is case sensitive. Hence hi is not equal to Hi. This works:

listofwords = ['hi','bye','yes','no']

String = 'hi how are you'
string2 = 'I have none of the words'

if any(ext in String for ext in listofwords):
   print(String)

2 Comments

There is no need to change string manually. Instead use lower to do same. What will you do when string is very long or entered by user input.
Well, depends on your use case I would say
0

The problem is both with case-sensitivity and with using in directly with a string.

If you want to make your search case-insensitive, consider converting both the String and the word to lower case, also, you should split the string after lower casing it, if you want to properly search for words:

if any(ext.lower() in String.lower().split() for ext in listofwords):
   print(String)

Splitting avoids returning True for strings like no in none and only works if no (or any other word) is present on its own. So now the above will work for both String (it will print it) and for string2 (it will not print it).

2 Comments

Thanks for taking this 1 step further. Would this also allow me to return the actual matched value in the string ->ext.lower()
@JarrattPerkins You can get the matched value, in fact there could be many values that exist in the list and in the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.