Python Forum
How to find exact matching string from the text
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find exact matching string from the text
#1
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
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
x = s.index('lowest')
Reply
#4
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"
Reply
#5
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
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split string into 160-character chunks while adding text to each part iambobbiekings 9 9,573 Jan-27-2021, 08:15 AM
Last Post: iambobbiekings
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Find string return different string Kristenl2784 2 1,529 Jul-23-2020, 04:31 AM
Last Post: ndc85430
  Appending to a text string syno7878 10 4,239 May-03-2020, 10:05 AM
Last Post: snippsat
  Using an integer to manipulate a string/text variable rexyboy2121 1 1,732 Apr-22-2020, 01:37 AM
Last Post: michael1789
  Trying to extract Only the capitol letters from a string of text Jaethan 2 2,168 Feb-27-2020, 11:19 PM
Last Post: Marbelous
  help with extracting and matching values in a text file hoeleeschitt 4 52,584 May-03-2018, 05:47 AM
Last Post: hoeleeschitt
  program that search string in text file and do something alon30 1 3,239 Aug-04-2017, 08:10 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020