3

Get the ASCII value of a character in Python

 1 year ago
source link: https://thispointer.com/get-the-ascii-value-of-a-character-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:

Advertisements

ord(character)
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'))
# 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 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)
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))
# 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
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)
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)
# 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)
# Get ASCII value for string 'thisPointer'
for i in 'thisPointer'.encode('ascii'):
    print(i)

Output:

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)
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)
# 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)
# Get ASCII value for string 'thisPointer'
for i in map(ord, 'thisPointer'):
    print(i)

Output:

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.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK