0
Names = ['James','Andrew','Red','Robert','Lucas','Lisa','Jack']

user_input = input('Enter an Alphabet:')
user_input = user_input.upper()

for A in Names:
    if user_input == A[0]:
        print(A)
martineau
  • 119,623
  • 25
  • 170
  • 301
Beginner
  • 19
  • 4

2 Answers2

2

In that code, A is a name from Names, and A[0] is a substring of just the first letter of the name. A[1] would be the 2nd letter, A[2] would be the third. A[-1] would be the last letter. A[:3] or A[0:3] would be the first 3 letters. Etc etc. To learn more you should look up python indexing and slicing.

So for this simple program, the user types a letter which is stored in user_input. Then, for every name, if the first letter of the name is the same as the typed letter, it prints the name. I.e., it prints out every name starting with the chosen letter.

almiki
  • 455
  • 2
  • 4
1

So, when you do

for A in Names:
    print(A[0], 'is the first character in', A, 'of type', type(A[0]))

output:
J is the first character in 'James' of type <class 'str'>
A is the first character in 'Andrew' of type <class 'str'>
R is the first character in 'Red' of type <class 'str'>
R is the first character in 'Robert' of type <class 'str'>
L is the first character in 'Lucas' of type <class 'str'>
L is the first character in 'Lisa' of type <class 'str'>
J is the first character in 'Jack' of type <class 'str'>

you are actually accessing each string of the strings in 'Names'. Thus, when you call A[0] you will get the first character of that string.

ottovon
  • 333
  • 2
  • 10