Python Program to find ASCII Code of a Character

In this Python example, you will learn how to Convert Characters to ASCII Code in Python

The abbreviation ASCII stands for American Standard Code for Information Interchange. It is a numerical value assigned to various symbols and characters, which allows computers to store and manipulate them. For instance, the ASCII value of the uppercase letter ‘A’ is 65.

# Prompt the user to enter a character
char = input("Enter a character: ")

# Find the ASCII value of the character using the ord() function
ascii_value = ord(char)

# Print the result
print(f"The ASCII value of '{char}' is {ascii_value}.")

Output

Enter a character: Z
The ASCII value of 'Z' is 90.

Real-world usages of character to ASCII conversion

Program to generate a character frequency histogram

# Python program to find ASCII value of character
 in a given string

# Define a string
text = "The quick brown fox jumps over the lazy dog"

# Define a dictionary to store character frequencies
freqs = {}

# Iterate over each character in the string
for char in text:
    # Check if the character is already in the dictionary
    if char in freqs:
        # If so, increment the frequency
        freqs[char] += 1
    else:
        # If not, add it to the dictionary with a frequency of 1
        freqs[char] = 1

# Print the ASCII values of each character and its frequency
for char, freq in freqs.items():
    ascii_value = ord(char)
    print(f"Character '{char}' (ASCII value: {ascii_value}) appears {freq} times.")

Output

Character 'T' (ASCII value: 84) appears 1 times.
Character 'h' (ASCII value: 104) appears 2 times.
Character 'e' (ASCII value: 101) appears 3 times.
Character ' ' (ASCII value: 32) appears 8 times.
Character 'q' (ASCII value: 113) appears 1 times.
Character 'u' (ASCII value: 117) appears 2 times.
Character 'i' (ASCII value: 105) appears 1 times.
Character 'c' (ASCII value: 99) appears 1 times.
Character 'k' (ASCII value: 107) appears 1 times.
Character 'b' (ASCII value: 98) appears 1 times.
Character 'r' (ASCII value: 114) appears 2 times.
Character 'o' (ASCII value: 111) appears 4 times.
Character 'w' (ASCII value: 119) appears 1 times.
Character 'n' (ASCII value: 110) appears 1 times.
Character 'f' (ASCII value: 102) appears 1 times.
Character 'x' (ASCII value: 120) appears 1 times.
Character 'j' (ASCII value: 106) appears 1 times.
Character 'm' (ASCII value: 109) appears 1 times.
Character 'p' (ASCII value: 112) appears 1 times.
Character 's' (ASCII value: 115) appears 1 times.
Character 'v' (ASCII value: 118) appears 1 times.
Character 't' (ASCII value: 116) appears 1 times.
Character 'l' (ASCII value: 108) appears 1 times.
Character 'a' (ASCII value: 97) appears 1 times.
Character 'z' (ASCII value: 122) appears 1 times.
Character 'y' (ASCII value: 121) appears 1 times.
Character 'd' (ASCII value: 100) appears 1 times.
Character 'g' (ASCII value: 103) appears 1 times.

Program to validate user input


# Prompt the user to enter a character
char = input("Enter a character: ")

# Validate user input
if len(char) != 1:
    print("Please enter a single character.")
else:
    # Get the ASCII value of the character
    ascii_value = ord(char)

    # Print the result
    print(f"The ASCII value of '{char}' is {ascii_value}.")

Output

Enter a character: 1
The ASCII value of '1' is 49.

Program to encode a string as a series of ASCII values


# Define a string
text = "Hello, world!"

# Encode the string as a series of ASCII values
ascii_values = [ord(char) for char in text]

# Print the encoded values
print("Encoded string:", ascii_values)

Output

Encoded string: [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]

Leave a Comment