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
char = 'A'
ascii_value = ord(char)
print(ascii_value) # Output: 65

Watch a course Python - The Practical Guide

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 in a string
string = "Hello"
ascii_value = ord(string[0])
print(ascii_value) # Output: 72