简体   繁体   中英

Check if string in list contains specific character and if it does only print that specific string from the list

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.

pretty simple:

for i in Names :
  if "a" in i : print( i )
else : print( "No names found" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM