Home >Backend Development >Python Tutorial >How do you get the ASCII value of a character in Python?

How do you get the ASCII value of a character in Python?

Linda Hamilton
Linda HamiltonOriginal
2024-11-21 10:33:091018browse

How do you get the ASCII value of a character in Python?

Getting the ASCII Value of a Character

In Python, you can retrieve the ASCII value of a character as an integer using the ord() function.

>>> ord('a')
97

To convert the ASCII value back to a character, you can use the chr() function.

>>> chr(97)
'a'

You can also perform arithmetic operations on ASCII values to manipulate characters. For instance, to get the character three positions to the right of 'a' in the ASCII sequence:

>>> chr(ord('a') + 3)
'd'

Note: In Python 2, the unichr() function was used to return Unicode characters. However, in Python 3, chr() can be used for both ASCII and Unicode characters.

The above is the detailed content of How do you get the ASCII value of a character in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn