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.
ord() - Python 3.6.5rc1 documentation
ord() - Python 2.7.14 documentation
Answer from Matt J on Stack OverflowFrom 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.
ord() - Python 3.6.5rc1 documentation
ord() - Python 2.7.14 documentation
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
Videos
I know that the PYTHON function chr(dec#) returns the ASCII character indicated by the dec#. My question is how would I return the specific decimal# of a selected character❓ Example: Open a FILE, read each character sequentially and return/assign the character's numerical ascii value (32-126) to variable as each character is read. The variable will then be used to create an ascii character value list of the input file.
Is there a PYTHON function similar to chr(dec#), that would return a numerical value of a specific ascii character❓
Thank You for any assistance.