Skip to content
Advertisement

python: How can I print an alphabet from a list, given a number?

Given a number from 1 to 26, I’m trying to return an alphabet in the corresponding position from a list.

Example:

Input = 1
Output = 'a'

I have tried the following:

user_input = int(input("Enter a number from 1 to 26 "))

for item in aplhabet:
    print(aplhabet.index(item[aplhabet]))

As expected that returns a type error because there are no integer values in my list.

What can I do to return an element from my list in which its position is equal to the users number input?

Advertisement

Answer

Using ASCII conversion from integer to character, you can do it as follows,

n = int(input("Enter the number: "))
if(n > 0 and n < 27):
    print(chr(n + 96))
else: 
    print("Invalid input")
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement