John Kindle
John Kindle

Reputation: 65

Python iteration program: print strings in a list that start with a specific character

Before I begin my question I would like to say that I am a beginner student taking an introduction to python course at my school, so my apologies if my question is not well worded, and suggestions would be welcome.

The question that I am struggling with is like so: "Create a program that requests a list of names from the user and print the names starting with A through I."

So far this is my code that I have had no luck with figuring out what I am doing wrong:

students = input('Enter a list of students: ')
for s in students:
    if students[:1] == 'abcdefghiABCDEFGHI':
        print(s)

Any answers would be greatly appreciated, thank you for your time in advance.

Upvotes: 1

Views: 184

Answers (3)

lvc
lvc

Reputation: 35059

Your problem appears to be here:

if students[:1] == 'abcdefghiABCDEFGHI':

This check will always be false. For str1 == str2 to be true, the strings have to be the same length, and have all the same characters in the same order. But students[:-1] is going to be either zero or one character long, so it can't ever be equal to the much longer string on the right. To check if that character is any of those ones in the long string, you can use the in operator:

if students[:1] in 'abcdefghiABCDEFGHI':

but note that this will be true if students is the empty string '', which might not be what you want. You can use students[0] instead to guard against that (the empty string will cause an error instead of a false positive), but a better way would be to use the str.startswith method like this:

if students.startswith(tuple('abcdefghiABCDEFGHI')):

The tuple call is there to turn the string into the tuple ('a', 'b', 'c', ...) - you might choose to put that tuple literal directly in your code instead. You need the argument to be a tuple, because startswith (like ==) checks strings of any length - but handing it a tuple of possible prefixes checks all of them individually and is true if any of them match.

Also, a more common way to do a case-insensitive check is to force the test string into the case you want; this is a trick that will come in handy when you have to test strings longer than one character - it works like this:

if students.upper().startswith(tuple("ABCDEFGHI")):

Upvotes: 1

BigZ
BigZ

Reputation: 886

def test(students=['Ferris','Douglas','Zarah','Jacob','Hamilton','Iris'], reg=r'[A-I][a-z]+'):
    import re
    for student in students:
        s = re.findall(reg, student)
        if s:
            print s[0]
test()

Upvotes: 0

GLHF
GLHF

Reputation: 4035

mylist = []
check = "a,b,c,d,e,f,g,h,i,A,B,C,D,E,F,G,H,I"
while True:
    students = input("Enter list of students: ")
    if students == "end":
        break
    mylist.append(students)

for x in mylist:
    if x[0] in check:
        print (x)

Output;

>>> 
Enter list of students: Jack
Enter list of students: Annie
Enter list of students: Foo
Enter list of students: Yo
Enter list of students: Ilsa
Enter list of students: gubar
Enter list of students: Ceren
Enter list of students: end
Annie
Foo
Ilsa
gubar
Ceren
>>> 

I used to "end" for breaking loop, so that I can append student names to a list and when I'm done, just wrote "end".

x[0] will check the first letter of student's name. If it's in our 'check' list, which is made by letters that we want, it will print it.

To be more clear, check this example;

mylist = []
check = "a,b,c,d,e,f,g,h,i,A,B,C,D,E,F,G,H,I"
while True:
    students = input("Enter list of students: ")
    if students == "end":
        break
    mylist.append(students)

print ("The name list of students: {}".format(mylist))
for x in mylist:
    if x[0] in check:
        print ("{} starts with {}, so printed.".format(x,x[0]))

>>> 
Enter list of students: Jack
Enter list of students: zed
Enter list of students: Ilsa
Enter list of students: Annie
Enter list of students: foo
Enter list of students: ceren
Enter list of students: tom
Enter list of students: end
The name list of students: ['Jack', 'zed', 'Ilsa', 'Annie', 'foo', 'ceren', 'tom']
Ilsa starts with I, so printed.
Annie starts with A, so printed.
foo starts with f, so printed.
ceren starts with c, so printed.
>>> 

Upvotes: 0

Related Questions