Q:

Python Program To Find ASCII value of a character

belongs to collection: Python Function Programs

0

In this tutorial, we will learn how to find the ASCII value of a character and display the result.

ASCII: ASCII is an acronym that stands for American Standard Code for Information Interchange. A specific numerical value is given to different characters and symbols for computers to store and manipulate in ASCII.

It is case sensitive. The same character, having different formats (upper case and lower case), has a different value. For example, The ASCII value of "A" is 65 while the ASCII value of "a" is 97.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

K = input("Please enter a character: ")    
    
print ("The ASCII value of '" + K + "' is ", ord(K))  

Output:

1#

Please enter a character: J
The ASCII value of 'J' is 74

2#

Please enter a character: $
The ASCII value of '$' is 36

In the above code, we have used the ord() function for converting a character into an integer, that is, ASCII value. This function is used to return the Unicode code point of the given character.

Example 2:

print ("Please enter the String: ", end = "")  
string = input()  
string_length = len(string)  
for K in string:  
    ASCII = ord(K)  
    print (K, "\t", ASCII)  

 

Output:

Please enter the String: 
 "JavaTpoint#
" 	 34
J 	 74
a 	 97
v 	 118
a 	 97
T 	 84
p 	 112
o 	 111
i 	 105
n 	 110
t 	 116
# 	 35

Unicode is also an encoding technique which is used for getting the unique number of the character. Though ASCII can only encode 128 characters, whereas the current Unicode can encode more than 100,000 characters from hundreds of scripts.

We can also convert the ASCII value into their corresponding character value. For that, we have to use the chr() in place of ord() in the above code.

Example 3:

K = 21    
J = 123  
R = 76  
print ("The character value of 'K' ASCII value is: ", chr(K))    
print ("The character value of 'J' ASCII value is: ", chr(J))    
print ("The character value of 'R' ASCII value is: ", chr(R))  

 

Output:

The character value of 'K' ASCII value is: 
The character value of 'J' ASCII value is: {
The character value of 'R' ASCII value is: L

Conclusion

In this tutorial, we have discussed how a user can convert character value into ASCII value and also how they can get the character value of the given ASCII value.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Program to Make a Simple Calculator... >>
<< Python Program to Convert Decimal to Binary, Octal...