How to print ASCII value in Python?

ASCII stands for American Standard Code for Information Interchange and it is a character encoding standard. Each character is assigned a unique numerical value known as ASCII value. In Python, you can easily print the ASCII value of a character using built-in functions and methods. Let’s explore different ways to print ASCII values in Python.

1. How to print the ASCII value using the ord() function?

The ord() function in Python returns the ASCII value of a character. You can pass the character as an argument to the ord() function and it will return the corresponding ASCII value.

character = 'A'
ascii_value = ord(character)
print("The ASCII value of", character, "is", ascii_value)

This will output:

The ASCII value of A is 65

2. How to print the ASCII value using the format() method?

You can use the format() method to print the ASCII value of a character. Specify the format specifier {} to include the ASCII value inside a string and use the format() method to replace the format specifier with the ASCII value.

character = 'B'
ascii_value = ord(character)
print("The ASCII value of {} is {}".format(character, ascii_value))

This will output:

The ASCII value of B is 66

3. How to print the ASCII values of multiple characters?

You can use a loop to print the ASCII values of multiple characters. Iterate through a string of characters and use the ord() function to get the ASCII value of each character.

characters = "XYZ"
for character in characters:
ascii_value = ord(character)
print("The ASCII value of", character, "is", ascii_value)

This will output:

The ASCII value of X is 88
The ASCII value of Y is 89
The ASCII value of Z is 90

4. How to print the ASCII value of a character in binary?

You can use the bin() function in Python to convert the ASCII value to binary. Append the prefix 0b to signify that the number is in binary format.

character = '!'
ascii_value = ord(character)
binary_value = bin(ascii_value)

print("The ASCII value of", character, "in binary is", binary_value)

This will output:

The ASCII value of ! in binary is 0b100001

5. How to print the ASCII value of a character in hexadecimal?

You can use the hex() function in Python to convert the ASCII value to hexadecimal. Append the prefix 0x to signify that the number is in hexadecimal format.

character = '9'
ascii_value = ord(character)
hex_value = hex(ascii_value)

print("The ASCII value of", character, "in hexadecimal is", hex_value)

This will output:

The ASCII value of 9 in hexadecimal is 0x39

6. How to print the ASCII value of a character in octal?

You can use the oct() function in Python to convert the ASCII value to octal. Append the prefix 0o to signify that the number is in octal format.

character = '%'
ascii_value = ord(character)
octal_value = oct(ascii_value)

print("The ASCII value of", character, "in octal is", octal_value)

This will output:

The ASCII value of % in octal is 0o45

7. How to find the character from an ASCII value?

You can use the chr() function in Python to find the character from an ASCII value. Pass the ASCII value as an argument to the chr() function and it will return the corresponding character.

ascii_value = 97
character = chr(ascii_value)

print("The character for ASCII value", ascii_value, "is", character)

This will output:

The character for ASCII value 97 is a

8. How to handle characters outside the ASCII range?

The ASCII range covers values from 0 to 127. If you try to find the ASCII value of a character outside this range, it will throw a TypeError: ord() expected a character error. To handle characters outside the ASCII range, you can use Unicode.

character = '€'
ascii_value = ord(character)

print("The ASCII value of", character, "is", ascii_value)

This will throw an error:

TypeError: ord() expected a character, but string of length 3 found

9. How to print the ASCII table in Python?

To print the ASCII table in Python, you can iterate through the range of ASCII values (0 to 127) and print the corresponding characters.

for ascii_value in range(128):
character = chr(ascii_value)
print("The ASCII value of", character, "is", ascii_value)

10. How to convert a string to ASCII values?

You can convert a string into a list of ASCII values using a loop. Iterate through each character in the string and use the ord() function to get the ASCII value of that character.

string = "Hello"
ascii_values = []

for character in string:
ascii_value = ord(character)
ascii_values.append(ascii_value)

print("The ASCII values of", string, "are", ascii_values)

11. How to convert a list of ASCII values to a string?

You can convert a list of ASCII values into a string using a loop. Iterate through each ASCII value in the list and use the chr() function to get the corresponding character.

ascii_values = [72, 101, 108, 108, 111]
string = ""

for ascii_value in ascii_values:
character = chr(ascii_value)
string += character

print("The string for ASCII values", ascii_values, "is", string)

12. How to print all printable ASCII characters in Python?

for ascii_value in range(32, 127):
character = chr(ascii_value)
print("The ASCII value of", character, "is", ascii_value)

This will output:

The ASCII value of   is 32
The ASCII value of ! is 33
The ASCII value of " is 34
...
The ASCII value of ~ is 126

Now you have learned various ways to print ASCII values in Python. Start exploring the ASCII values of different characters and create wonderful programs!

Dive into the world of luxury with this video!


Your friends have asked us these questions - Check out the answers!

Leave a Comment