Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieve word from string
#1
Good day !

Based on the example below how could i print the word "fruits" ?
considering that it may change the position in the sentence and
considering that i have only "fr" as a search key



phrase = "I'm selling fruits and vegetables"

if "fr" in phrase:
...
Reply
#2
How about:

phrase = "I'm selling fruits and vegetables"

phrase_lst = phrase.split()
for word in phrase_lst:
    if word.startswith("fr"):
        print(word)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
There are several ways to do this. One is to use string find() method and then check where the next space is. Another way is to use regular expressions, which will be faster if you have a large text and you want to find all occurrences. You can also try if 'fr' in your_sentence code in case you just want to check membership.
Reply
#4
Example using re
import re

phrase = "I'm selling fruits and vegetables from France"
for match in re.finditer(r"([Ff]r\S*)", phrase):
    print(match)
Output:
<re.Match object; span=(12, 18), match='fruits'> <re.Match object; span=(34, 38), match='from'> <re.Match object; span=(39, 45), match='France'>
If you only want the words.
import re

phrase = "I'm selling fruits and vegetables from France"
print(re.findall(r"([Ff]r\S*)", phrase))
Output:
['fruits', 'from', 'France']
Reply
#5
re always confuses me!

re.search() finds the first instance of your match in phrase, so I tried re.search('fr(?=uits)', phrase) This will find fr if it is followed by uits:

import re

phrase = "I'm selling French fruit and vegetables from fruity France fruits"
# match fr followed by uits
m = re.search('fr(?=uits)', phrase)
m
Output:
<re.Match object; span=(59, 61), match='fr'>
phrase = "I'm selling French fruits and vegetables from fruity France fruits"
m = re.search('fr(?=uits)', phrase)
m
Output:
<re.Match object; span=(19, 21), match='fr'>
pos = m.span() 
will get you the index positions of fr, like (19, 21) should you need them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 550 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Isolate a word from a long string nicocorico 2 1,545 Feb-25-2022, 01:12 PM
Last Post: nicocorico
  change string in MS word Mr_Blue 8 3,352 Sep-19-2021, 02:13 PM
Last Post: snippsat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,518 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 16,027 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  filter just with the string word jacklee26 2 2,415 Feb-03-2020, 03:25 PM
Last Post: snippsat
  Reverse the string word sneha 2 2,644 Dec-12-2019, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 7,135 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  print a word after specific word search evilcode1 8 4,866 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  Need help to retrieve a particular string lokesh 3 1,756 May-28-2019, 05:07 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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