Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the ASCII value of a character

Tags:

python

ascii

How do I get the ASCII value of a character as an int in Python?

like image 864
Matt Avatar asked Oct 22 '08 20:10

Matt


People also ask

How do I find the ASCII value of a character?

Program to Print ASCII ValueThe character is stored in variable c . When %d format string is used, 71 (the ASCII value of G ) is displayed. When %c format string is used, 'G' itself is displayed.

How do I get the ASCII value of a character in Word?

To search for an ASCII character, enter a carat (^) followed by the three numbers representing the ASCII value of the character. For instance, to search for an uppercase A, you could enter ^065. Set other searching parameters, as desired.

How do I find the ASCII value of a string?

ASCII value of a String is the Sum of ASCII values of all characters of a String. Step 1: Loop through all characters of a string using a FOR loop or any other loop. Step 3: Now add all the ASCII values of all characters to get the final ASCII value. A full example program is given below.


1 Answers

From here:

The function ord() gets the int value of the char. And in case you want to convert back after playing with the number, function chr() does the trick.

>>> ord('a') 97 >>> chr(97) 'a' >>> chr(ord('a') + 3) 'd' >>> 

In Python 2, there was also the unichr function, returning the Unicode character whose ordinal is the unichr argument:

>>> unichr(97) u'a' >>> unichr(1234) u'\u04d2' 

In Python 3 you can use chr instead of unichr.


ord() - Python 3.6.5rc1 documentation

ord() - Python 2.7.14 documentation

like image 127
Matt J Avatar answered Oct 03 '22 08:10

Matt J