stobbart aidan
stobbart aidan

Reputation: 51

How do you print values from a list starting with specifc characters

I have only started learning python hence the very basic question!

So I just started learning the basics of python and right now I am covering lists and how they work. I would like to print values in my list that start with the letters "F", "P" and "E".

My list consists of the names of all the countries in the EU. Here is what I have come up with, but unfortunetly doesn't work as expected, instead it prints nothing.

Special_Characters = ("FPE")
for characters in member_states:         #member_states being my list of countries
    if characters in Special_Characters:
        print (characters)

If you have a solution to my problem could you let me know where I went wrong so I can learn from it.

Upvotes: 3

Views: 46

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

Just check the first letter of the country (if it's in special characters) not the entire word:

Special_Characters = ("FPE")
for characters in member_states:         #member_states being my list of countries
    if characters[0] in Special_Characters: #with characters[0] we check only the first letter
        print (characters)

Upvotes: 3

Related Questions