0

If I have a list:

mylist = ['super mario brothers',
          'animal crossing',
          'legend of zelda breath of the wild',
          'kirby superstar ultra']

Can I make super mario brothers to be printed if the user inputs mario, legend of zelda breath of the wild to be printed if user inputs zelda, and kirby superstar ultra to be printed if the user inputs star?

S3DEV
  • 8,768
  • 3
  • 31
  • 42
kim
  • 17
  • 3
  • Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – Pranav Hosangadi Nov 12 '20 at 17:47
  • Sure. Can you please update the question to show what you’ve tried / researched so far; and specifically where you are stuck? – S3DEV Nov 12 '20 at 17:48
  • Since you are new you may not know, but if your question has been answered please accept the answer that has answered your question by giving it a tick, if the link in the comments answers your question then please close the answer. – coderoftheday Nov 12 '20 at 18:00

5 Answers5

0

You can check whether a string contains another string with in:

>>> 'mario' in 'super mario brothers'
True

So:

given_name = 'mario'

for sentence in mylist:
    if given_name in sentence:
        print(sentence)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

Take the input as ans

Now iterate through your list, for each sentence in your list, see if the sentence contains ans, if so print the sentence.

mylist = ['super mario brothers', 'animal crossing', 'legend of zelda breath of the wild', 'kirby superstar ultra']

ans = input('Enter name')

for title in mylist:
    if ans in title:
        print(title)
coderoftheday
  • 1,987
  • 4
  • 7
  • 21
0

You can use the in operator.

I took the liberty of also adding case-insensitivity by lowercasing both the game name and the user input.

list_of_games = [
    "Super Mario Brothers",
    "Animal Crossing",
    "Legend of Zelda Breath of the Wild",
    "Kirby Superstar Ultra",
]
search_string = input("Search for a game:").lower()
for title in list_of_games:
    if search_string in title.lower():
        print(title)

As discussed in the comments, if you'd like to handle things differently depending on how many games match the input, we can change things e.g. so:

search_string = input("Search for a game:").lower()
# Build up a list of matching games using a list comprehension
matching_games = [title for title in list_of_games if search_string in title.lower()]
if not matching_games:  # the list is falsy if it's empty
    print("No matches for that input, sorry!")
elif len(matching_games) == 1:  # Only one match
    print("One match:", matching_games[0])
else:
    print("Multiple matches:")
    for title in matching_games:
        print("*", title)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • hi, thank you for your kind reply. I was wondering if a user types in something completely unrelated to the titles in the list, is it possible to print something that tells the user that the game they have entered is not on the list? if I add a line "else: print("entered game is not on the list") "after print(title), the code just prints entered game is not on the list regardless of what I put in the input. – kim Nov 13 '20 at 18:47
  • @kim Sure thing. I added an example. – AKX Nov 13 '20 at 18:53
0

Or, just for a laugh ... here’s a one-liner:

[i if i.find('mario') > -1 else None for i in mylist][0]

Essentially, this checks each element in the list for the search string and returns the first matching list element if found; otherwise None is returned.

S3DEV
  • 8,768
  • 3
  • 31
  • 42
-2

You could use regex, iterating over all the elements in the list

IlMio Fake
  • 77
  • 6