Python Forum

Full Version: How to find exact matching string from the text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here

Dear ALL,

Can anyone help me how to find a word or phrase that occurs in the text without using splitter command?


waiting for your answer and thanks 

but I need without splitting

my code here
str.find(substring) returns the index of the first occurrence in the string.

In [1]: s = """Return the lowest index in S where substring sub is found,
   ...: such that sub is contained within S[start:end].  Optional
   ...: arguments start and end are interpreted as in slice notation."""

In [2]: s.find('lowest')
Out[2]: 11
Ref: https://docs.python.org/3.5/library/stdt...l#str.find
x = s.index('lowest')
BY USING (index and find ), we able to find exact and some part of the word also. But, I want only exact .

for example,

s= 'Sports drinks seem to have become a norm in the fitness world. '


t= sports

it should return yes , but when I search
t= "spor"
it should return "no"
Use code tag.
You can just check with in.
>>> s = 'Sports drinks seem to have become a norm in the fitness world.'
>>> t = 'sports'
>>> if t in s.lower().split():
...     print('Yes')
... else:    
...     print('No')
...     
Yes

>>> t = 'spor'
>>> if t in s.lower().split():
...     print('Yes')
... else:    
...     print('No')
...     
No
Moved thread to Homework, since this is obviously homework.
Your question's been answered by several people, using several methods. If that doesn't work for you, please explain why.