Skip to content

How to get the ASCII value of a character

To get the ASCII value of a character in Python, you can use the built-in ord() function. The ord() function takes a single character as an argument and returns the corresponding ASCII value.

Here is an example code snippet:

Get the ASCII value of a character in Python

python
# Get the ASCII value of a character
char = 'A'
ascii_value = ord(char)
print(ascii_value) # Output: 65

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

You can also use the ord() function with a string, but it will only return the ASCII value of the first character in the string.

Get the ASCII value of the first character of a string in Python

python
# Get the ASCII value of the first character in a string
string = "Hello"
ascii_value = ord(string[0])
print(ascii_value) # Output: 72

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.