What is wrong with:
if word in mystring:
print('success')
Answer from fabmilo on Stack OverflowWhat is wrong with:
if word in mystring:
print('success')
if 'seek' in 'those who seek shall find':
print('Success!')
but keep in mind that this matches a sequence of characters, not necessarily a whole word - for example, 'word' in 'swordsmith' is True. If you only want to match whole words, you ought to use regular expressions:
import re
def findWholeWord(w):
return re.compile(r'\b({0})\b'.format(w), flags=re.IGNORECASE).search
findWholeWord('seek')('those who seek shall find') # -> <match object>
findWholeWord('word')('swordsmith') # -> None
Python - If string contains a word from a list or set - Stack Overflow
python - Determining if a string contains a word - Stack Overflow
Python how to check if a string contains a word - Stack Overflow
Python - Check if a string contains multiple words - Stack Overflow
Swear = ["curse", "curse", "curse"]
for i in Swear:
if i in Userinput:
print 'Quit Cursing!'
You should read up on the differences between lists and tuples.
You can use sets, if only you want to check the existance of swear words,
a_swear_set = set(Swear)
if a_swear_set & set(Userinput.split()):
print("Quit Cursing!")
else:
print("That sounds great!")
words = 'blue yellow'
if 'blue' in words:
print 'yes'
else:
print 'no'
Also nightly blues would contain blue, but not as a whole word. If this is not what you want, split the wordlist:
if 'blue' in words.split():
โฆ
You can use in or do explicit checks:
if 'blue ' in words:
print 'yes'
or
if words.startswith('blue '):
print 'yes'
Edit: Those 2 will only work if the sentence doesnt end with 'blue'. To check for that, you can do what one of the previous answers suggested
if 'blue' in words.split():
print 'yes'
This should work - it will ensure only whole words are matched, and sentences must match from the start.
def string_contains(str1,str2):
lst1 = str1.split(' ')
lst2 = str2.split(' ')
if len(lst1) <= len(lst2):
return lst1 == lst2[:len(lst1)]
return False
print (string_contains("llo", "hello how are you")) # False
print (string_contains("hello", "hello how are you")) # True
print (string_contains("hello how", "hello how are you")) # True
print (string_contains("hello how a", "hello how are you")) # False
I like using regular expression module re.
Code:
import re
pattern = re.compile('\sllo\s') # add another parameter `re.I` for case insensitive
match = pattern.search('hello how are you')
if match:
return True
else:
return False
Basic implementation
Generally, you use split() to split a string of words into a list of them. If the list has more than one element, it's True (i.e. you could print yes)
def contains_multiple_words(s):
return len(s.split()) > 1
Punctuation support
To support punctuation etc as well, you can split on a regular expression, via the re module's split command:
import re
def contains_multiple_words(s):
return len(re.compile('\W').split(s)) > 1
The regular expression character class \W means any single non-word character, e.g. punctuation or spaces (see the Python regex syntax guide for details).
Thus, splitting on this instead of just space (the default in the first example) allows for a more realistic idea of "words".
No there don't exsist any mechanism of the shelf like this.
If your only aim is to find if it's only one word or there exsit another word it can be done by :
x = 'has words'
' ' in x
>>> True