Python Program to Print Nth Letter of Alphabets

In this article, we will learn about python program to print nth letter of alphabets in lowercase or uppercase.

Getting Started

The task is to print nth letter of alphabets in lowercase or uppercase. For example,

If n = 10 in lowercase, then output will be – j.
If n = 10 in uppercase, then output will be – J.

We can achieve above task in multiple ways –

Using ASCII Values

We can use ASCII values of alphabets to print nth letter.

Pseudo Algorithm

  • Get the nth position value by user and store in a variable – n.
  • Now, add ASCII value of first letter in alphabet and (n – 1). We already know that ascii value of first letter in lowercase and uppercase are 97 and 65 respectively.
  • Now, get the character using chr() function. Then, print it using print() function.
  1. Print nth alphabet in lowercase

    For lowercase, ASCII value of first character is 97. So, program is –

    n = int(input("Enter nth value:"))
    asciiValue = 97 + (n - 1)
    print("Character is: " + chr(asciiValue))
    

    Output:

    Enter nth value:
    16
    Character is: p
    
  2. Print nth alphabet in uppercase

    For uppercase, ASCII value of first character is 65. So, program is –

     
    n = int(input("Enter nth value:"))
    asciiValue = 65 + (n - 1)
    print("Character is: " + chr(asciiValue))
    

    Output:

     
    Enter nth value:
    10
    Character is: J
    
  3. Using string.ascii_lowercase or string.ascii_uppercase

    String constant, string.ascii_lowercase and string.ascii_uppercase, returns all alphabets in lowercase and uppercase respectively. We can use it to print nth character in alphabets in python.

    Pseudo Algorithm

    • Store returned value by string.ascii_lowercase or string.ascii_uppercase in a variable i.e. alphabets.
    • Get the nth value by user and store in a variable i.e. n
    • (n-1)th position element in alphabets are value which are are interested in.
    • Now, get the (n-1)th position element using indexing and print it using print() function.
    1. Print Letter in Lowercase using string.ascii_lowercase

      import string
      n = int(input("Enter nth value:"))
      alphabets = string.ascii_lowercase
      character = alphabets[n - 1]
      print("Character is: " + character)
      

      Output:

      Enter nth value:
      5
      Character is: e
      
    2. Print Letter in Uppercase using string.ascii_uppercase

      import string
      n = int(input("Enter nth value:"))
      alphabets = string.ascii_uppercase
      character = alphabets[n - 1]
      print("Character is: " + character)
      

      Output:

      Enter nth value:
      6
      Character is: F
      

    Using ord() and chr() Functions

    ord() function returns ASCII value of any character. chr() function returns character for any ASCII value.

    In other words, we can say that ord() and chr() function are complement of each other.

    We can use both functions to find and print nth character in alphabets.

    Pseudo Algorithm

    • Get the nth value by user using input() function.
    • Then, get the ASCII value of first letter of alphabet using ord() function.
    • After that, add n – 1 to ascii value of first letter.
    • Now, we have ASCII value of nth letter in alphabet. Get the corresponding character using chr() function and print it using print() function.
    1. Print Nth letter in Lowercase

       
      n = int(input("Enter nth value:"))
      character = chr(ord('a') + (n - 1))
      print("Character is: " + character)
      

      Output:

       
      Enter nth value:
      8
      Character is: h
      
    2. Print Nth letter in Uppercase

       
      n = int(input("Enter nth value:"))
      character = chr(ord('A') + (n - 1))
      print("Character is: " + character)
      

      Output:

       
      Enter nth value:
      16
      Character is: P
      

    Using string.letters

    We can also use string.letters to write python program to print nth letter of alphabets.
    String constant string.letters returns lowercase and uppercase letters of alphabets as below –

    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
    

    Notice that lowercase letters are before uppercase letters.

    We can use this feature to find and print nth letters of alphabets.

    Pseudo Algorithm

    • Get the nth value by user using input() function.
    • For lowercase, (n – 1)th position in value returned by string.letters is our desired letter
    • For uppercase, (n – 1 + 26)th position letter is our desired letter
    1. Print Letter in Lowercase

       
      import string
      n = int(input("Enter nth value:"))
      character = string.ascii_letters[n - 1]
      print("Character is: " + character)
      

      Output:

       
      Enter nth value:
      17
      Character is: q
      
    2. Print Letter in Uppercase

       
      import string
      n = int(input("Enter nth value:"))
      character = string.ascii_letters[n - 1 + 26]
      print("Character is: " + character)
      

      Output:

       
      Enter nth value:
      15
      Character is: O
      

    That’s how we can write python program to print nth letter in alphabets.

    Reference – Official Doc

Leave a Reply