Python program to find ASCII value of a character

Here, we are going to learn how to print the ASCII value of a given character in Python? By IncludeHelp Last updated : April 09, 2023

Problem statement

Given a character, and we have to find its ASCII code using Python program.

ASCII value of character in Python

In Python, to get the ASCII value of a character, we use ord() function. The ord() accepts a character and returns the ASCII value of it.

Syntax

ord(character);

Example

Consider the below example with sample input and output:

Input:
char_var = 'A'

Function call:
ord(char_var)

Output:
65

Python program to find ASCII value of a character

# python program to print ASCII
# value of a given character

# Assigning character to a variable
char_var = 'A'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))

char_var = 'x'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))

char_var = '9'
# printing ASCII code
print("ASCII value of " + char_var + " is = ", ord(char_var))

Output

ASCII value of A is =  65
ASCII value of x is =  120
ASCII value of 9 is =  57

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.