0

I want to print out the entire string if it contains a particular word. for example

a = ['www.facbook.com/xyz','www.google.com/xyz','www.amazon.com/xyz','www.instagram.com/xyz']

if I am looking to find the word amazon then the code should print www.amazon.com/xyz

I have found many examples in which you can find out if a string contains a word but I need to print out the entire string which contains the word.

think-maths
  • 917
  • 2
  • 10
  • 28
Gaurav
  • 11
  • 2
  • If you have found a way to find out if a string contains a word or not, you can just use this information and access the processed entry in the list and print it then ;-) `for entry in a: if EntryContainsWord(entry, 'word'): print(entry)` (Sorry for the bad formatting ;-) ) – Markus Safar Mar 11 '21 at 09:56
  • 1
    Does this answer your question? [Check if a Python list item contains a string inside another string](https://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string) – think-maths Mar 11 '21 at 10:00
  • @GauravVerma Feel free to accept an answer if you found it helpful. (Click on the grey checkmark next to the post). – Justin Mar 15 '21 at 11:33

2 Answers2

0

Try this -

your_list = ['www.facebook.com/xyz', 'www.google.com/xyz', 'www.amazon.com/xyz', 'www.instagram.com/xyz']
word = 'amazon'
res = [x for x in your_list if word in x] 
print (*res)

Output:

www.amazon.com/xyz
Justin
  • 1,006
  • 12
  • 25
0

This works fine if there are only one or two strings containing the word, if there are multiple strings in the list containing that name it will print them in a horizontal line. It needs to print line by separate line but I do not know how to incorporate this in the code. It would be interesting to see how that looks.

lionel
  • 19
  • 4