Python - Check if there are some word inside a sting | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python - Check if there are some word inside a sting

I want to check if a string contains one or more specific words inside it. Normally i will do this: If "hi" in str or "hello" in str: print('true') Any way to make it smaller? I have tried to do this but it wont work If "hi", "hello" in str: print('true')

1st May 2019, 7:06 AM
Nopparuj
Nopparuj - avatar
2 Answers
+ 3
for greeting in long_list_of_greetings: if greeting in string: print('true')
1st May 2019, 7:11 AM
HonFu
HonFu - avatar
+ 1
Add a break to Honfu's solution since you only want "True" to be printed once. OR if any(gretting in string for greeting in long_list_of greetings): print("True")
1st May 2019, 7:36 AM
Diego
Diego - avatar