Get the ASCII value of a character in Python

In this Python tutorial, you will learn how to get the ASCII value of a character and vice-versa.

Table Of Contents

Let’s dive into the tutorial.

Get ASCII value of a character using ord()

The ord() function is used to return the ASCII value for the given character or the given string.

Syntax:

ord(character)

Parameters:

It takes a character as the only parameter.

Example:

In this example, we will get the ASCII values of the following characters, A,y,v,M,a

# Return the ASCII value of character - 'A'
print('ASCII value of character - A: ',ord('A'))

# Return the ASCII value of character - 'y'
print('ASCII value of character - y: ',ord('y'))

# Return the ASCII value of character - 'v'
print('ASCII value of character - v: ',ord('v'))

# Return the ASCII value of character - 'M'
print('ASCII value of character - M: ',ord('M'))

# Return the ASCII value of character - 'a'
print('ASCII value of character - a: ',ord('a'))

Output:

ASCII value of character - A:  65
ASCII value of character - y:  121
ASCII value of character - v:  118
ASCII value of character - M:  77
ASCII value of character - a:  97

ASCII values for the above characters are returned.

Suppose, if you want to return characters based on the ASCII values, you can use the chr() function.

Syntax:

chr(ASCII-value)

Parameters:

It takes ASCII-value as the only parameter.

Example:
In this example, we will get the characters of the following ASCII values, 65,121,118,77,97.

# Return the  character for the ASCII value - 65
print('character for the ASCII value - 65: ',chr(65))

# Return the  character for the ASCII value - 121
print('character for the ASCII value - 121: ',chr(121))

# Return the  character for the ASCII value - 118
print('character for the ASCII value - 118: ',chr(118))

# Return the  character for the ASCII value - 77
print('character for the ASCII value - 77: ',chr(77))

# Return the  character for the ASCII value - 97
print('character for the ASCII value - 97: ',chr(97))

Output:

character for the ASCII value - 65:  A
character for the ASCII value - 121:  y
character for the ASCII value - 118:  v
character for the ASCII value - 77:  M
character for the ASCII value - 97:  a

Characters for the above ASCII values are returned.

Get ASCII value of a character using encode()

The encode() function of string class, takes ‘ascii’ as a parameter to yields the ASCII value of all characters in the string. It can be used with for loop so that we want to get the ASCII values of all characters in a string. Now, we can keep the single character in string and get its ASCII value using encode() function.

Syntax:

for iterator in 'character'.encode('ascii'):
    print(iterator)
  1. The character is the input character
  2. The iterator is used to return only the ASCII value from the encoded values.

Example:

In this example, we will return the ASCII value from the given character – A.

# Get ASCII value for character A
for i in 'A'.encode('ascii'):
    print(i)

Output:

65

It is also possible to return ASCII values from a string.

Example:

In this example, we will return ASCII values from the given string – ‘thisPointer’.

# Get ASCII value for string 'thisPointer'
for i in 'thisPointer'.encode('ascii'):
    print(i)

Output:

116
104
105
115
80
111
105
110
116
101
114

Get ASCII value of a character using map()

The map() function will take ord() and a string as parameters and applies the ord() function on all characters in string. Then returns a sequence of results i.e. ASCII values of characters in string. In our case, the map() function will take ord() and a string with single character as parameters. It will return the ASCII value of the given character. It can be used with for loop so that we can return ASCII value.

Syntax:

for iterator in map(ord, 'character'):
    print(iterator)
  1. The character is the input character
  2. The iterator is used to return only the ASCII value from the encoded values.

Example:

In this example, we will return ASCII value from the given character – A.

# Get ASCII value for character - 'A'
for i in map(ord, 'A'):
    print(i)

Output:

65

It can be possible to return ASCII values from a string.

Example:

In this example, we will return ASCII values from the given string – ‘thisPointer’.

# Get ASCII value for string 'thisPointer'
for i in map(ord, 'thisPointer'):
    print(i)

Output:

116
104
105
115
80
111
105
110
116
101
114

Summary

From the tutorial, we have seen how to return the ASCII value of the given character using the ord() function. Also, the map() used ord as a parameter to get ASCII value from the given character. If you want to return a character from the given ASCII value, you can use the chr() function. Using encode(), we also returned the ASCII value. Happy Learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top