asked 57.2k views
3 votes
How could I compare the 1st letter of an element in the list of name with the letter inputted. (In Python)

How could I compare the 1st letter of an element in the list of name with the letter-example-1
asked
User Trae
by
8.6k points

1 Answer

3 votes
To compare the first letter of an element in a list of names with a letter inputted in Python, you can use a for loop to iterate over the list of names. For each element in the list, you can use the str.startswith() method to check if the element's name starts with the letter inputted. For example:
names = ["John", "Jane", "David", "Alex"]
letter = "J"

for name in names:
if name.startswith(letter):
print(name)
answered
User Martijn Dashorst
by
8.4k points