Check if a word is in a string in Python
What is wrong with:
if word in mystring:
print('success')
How to check if a word is an English word with Python?
For (much) more power and flexibility, use a dedicated spellchecking library like PyEnchant
. There's a tutorial, or you could just dive straight in:
>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]
>>>
PyEnchant
comes with a few dictionaries (en_GB, en_US, de_DE, fr_FR), but can use any of the OpenOffice ones if you want more languages.
There appears to be a pluralisation library called inflect
, but I've no idea whether it's any good.
Python - Check if a word is in a string
What about to split the string and strip words punctuation, without forget the case?
w in [ws.strip(',.?!') for ws in p.split()]
Maybe that way:
def wordSearch(word, phrase):
punctuation = ',.?!'
return word in [words.strip(punctuation) for words in phrase.split()]
# Attention about punctuation (here ,.?!) and about split characters (here default space)
Sample:
>>> print(wordSearch('Sea'.lower(), 'Do seahorses live in reefs?'.lower()))
False
>>> print(wordSearch('Sea'.lower(), 'Seahorses live in the open sea.'.lower()))
True
I moved the case transformation to the function call to simplify the code...
And I didn't check performances.
Checking if a word does not exist in a string Python
Checking if any element is not in message.
You could try this.
x = ['hello','there']
msg = 'hello sir'
wordsNotExistant = [words for words in msg.split(' ') if words not in x]
Output
['sir']
All the words in the given message that cannot be found in x
will be appended to wordNotExistant
Check list of words in another string
if any(word in 'some one long two phrase three' for word in list_):
Determining if a string contains a word
words = 'blue yellow'
if 'blue' in words:
print 'yes'
else:
print 'no'
Edit
I just realized that 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():
…
How does Python check if a word is in a string?
You are not checking the condition right. You should rather
print('this' in res and 'that' in res and 'and' in res)
What you are checking is actually "true" in Python. Refer to this example below to find out
if 'this':
print('found') # will print found
Check if string contains a word from dictionary python
You can use re
:
import re
names = ["Joe", "Smith", "Nancy"]
string = "Her name was Nancy. His name was Smith"
result = re.findall('|'.join(names), string)
print(*result, sep='\n')
Nancy
Smith
Related Topics
Add Custom Method to String Object
Distributing Ruby/Python Desktop Apps
How to Deploy a Perl/Python/Ruby Script Without Installing an Interpreter
Unicodedecodeerror, Invalid Continuation Byte
How Is Python's List Implemented
Convert Binary to Ascii and Vice Versa
Valueerror: Setting an Array Element with a Sequence
List Comprehension Rebinds Names Even After Scope of Comprehension. Is This Right
Passing Extra Arguments Through Connect
Why Aren't Python Nested Functions Called Closures
How to Generate Dynamic (Parameterized) Unit Tests in Python
Construct Pandas Dataframe from Items in Nested Dictionary
Scheduling a Regular Event: Cron/Cron Alternatives (Including Celery)
Pytest Running with Another Version of Python
Why Does Loading the Libc Shared Library Have "'Libraryloader' Object Is Not Callable" Error