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

Linda Hamilton
Release: 2024-11-21 10:33:09
Original
968 people have browsed it

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
Copy after login

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

>>> chr(97)
'a'
Copy after login

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'
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template