Python Tutorials

Python Programs

Python Find ASCII Value of a Character


Python Find ASCII Value of a Character

In this tutorial, we will learn how to find the ASCII value of a character in Python. We will cover the basic concept of ASCII values and implement a function to perform the conversion.


What is an ASCII Value

ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a numerical value to each character. For example, the ASCII value of 'A' is 65 and 'a' is 97.


Syntax

The syntax to find the ASCII value of a character in Python is:

def char_to_ascii(char):
    return ord(char)


Finding the ASCII value of a character

We can create a function to find the ASCII value of a given character using Python's built-in functions.

For example,

  1. Define a function named char_to_ascii that takes one parameter char.
  2. Use the built-in ord function to get the ASCII value of the character.
  3. Return the ASCII value.
  4. Call the function with a sample character and print the result.

Python Program

def char_to_ascii(char):
    return ord(char)

# Find the ASCII value of 'A'
result = char_to_ascii('A')

# Print the result
print('ASCII value of A is', result)

Output

ASCII value of A is 65