In this tutorial, we will see how to find the ASCII value of a character. To find the ASCII value of a character, we can use the ord() function, which is a built-in function in Python that accepts a char (string of length 1) as argument and returns the unicode code point for that character. Since the first 128 unicode code points are same as ASCII value, we can use this function to find the ASCII value of any character.
Program to find the ASCII value of a character
In the following program, user enters the character and the program returns the ASCII value of input character.
# Program to find the ASCII value of a character ch = input("Enter any character: ") print("The ASCII value of char " + ch + " is: ",ord(ch))
Output:
Program to find the character from a given ASCII value
We can also find the character from a given ASCII value using chr() function. This function accepts the ASCII value and returns the character for the given ASCII value.
# Program to find the character from an input ASCII value # getting ASCII value from user num = int(input("Enter ASCII value: ")) print(chr(num)) # ASCII value is given num2 = 70 print(chr(num2))
Output:
Related Python Examples
1. Python program to find sum of n natural numbers
2. Python program to add digits of a number
3. Python program to convert decimal to hexadecimal
4. Python program to print calendar
Leave a Reply