0

So let's say I have a list of names and I want to see if they contain the letter "a". How would I go about printing just the strings in the list containing that and not just printing the whole list.

Names = ["Mike", "Ted", "John", "Adam", "Liam"]
if ("a" in Names):
     print (//Print out the names containing a//)

else:
     if ("a" not in Names):
     print("No names found containing a")

Obviously it's a bit more complicated than this but just to show just a very basic look at what I'm aiming for.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Jamie
  • 19
  • 1
  • 1
  • 4
  • Do you understand, in general, how to do something `for` each item that is `in` the list? Hint, hint. – Karl Knechtel Apr 17 '21 at 00:37
  • 1
    Does this answer your question? [How to check a string for specific characters?](https://stackoverflow.com/questions/5188792/how-to-check-a-string-for-specific-characters) – Woodford Apr 17 '21 at 00:43
  • you can try ok = [x for x in Names if 'a' in x]. you might want to adjust it to make it more dynamic. but this should show you how – Ade_1 Apr 17 '21 at 00:43

1 Answers1

0

pretty simple:

for i in Names :
  if "a" in i : print( i )
else : print( "No names found" )
gXLg
  • 184
  • 13