'How to get the ASCII value of a character
How do I get the ASCII value of a character as an int in Python?
Solution 1:[1]
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, functionchr()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.
Solution 2:[2]
Note that ord() doesn't give you the ASCII value per se; it gives you the numeric value of the character in whatever encoding it's in. Therefore the result of ord('รค') can be 228 if you're using Latin-1, or it can raise a TypeError if you're using UTF-8. It can even return the Unicode codepoint instead if you pass it a unicode:
>>> ord(u'?')
12354
Solution 3:[3]
You are looking for:
ord()
Solution 4:[4]
The accepted answer is correct, but there is a more clever/efficient way to do this if you need to convert a whole bunch of ASCII characters to their ASCII codes at once. Instead of doing:
for ch in mystr:
code = ord(ch)
or the slightly faster:
for code in map(ord, mystr):
you convert to Python native types that iterate the codes directly. On Python 3, it's trivial:
for code in mystr.encode('ascii'):
and on Python 2.6/2.7, it's only slightly more involved because it doesn't have a Py3 style bytes object (bytes is an alias for str, which iterates by character), but they do have bytearray:
# If mystr is definitely str, not unicode
for code in bytearray(mystr):
# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):
Encoding as a type that natively iterates by ordinal means the conversion goes much faster; in local tests on both Py2.7 and Py3.5, iterating a str to get its ASCII codes using map(ord, mystr) starts off taking about twice as long for a len 10 str than using bytearray(mystr) on Py2 or mystr.encode('ascii') on Py3, and as the str gets longer, the multiplier paid for map(ord, mystr) rises to ~6.5x-7x.
The only downside is that the conversion is all at once, so your first result might take a little longer, and a truly enormous str would have a proportionately large temporary bytes/bytearray, but unless this forces you into page thrashing, this isn't likely to matter.
Solution 5:[5]
To get the ASCII code of a character, you can use the ord() function.
Here is an example code:
value = input("Your value here: ")
list=[ord(ch) for ch in value]
print(list)
Output:
Your value here: qwerty
[113, 119, 101, 114, 116, 121]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | smci |
| Solution 2 | Xantium |
| Solution 3 | Jacob Krall |
| Solution 4 | Isma |
| Solution 5 | Indi |
