Skip to content
Fix Code Error

How to get the ASCII value of a character

March 13, 2021 by Code Error
Posted By: Matt

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

Solution

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

Answered By: Matt J

Related Articles

  • BackboneJS best way to rearrange models in a collection…
  • no match for ‘operator
  • Error: Each row of output must be identified by a unique…
  • PHP: How to remove all non printable characters in a string?
  • Fastest way to iterate over all the chars in a String
  • Getting weird compilation error in defining a std::set with…
  • Calculate a Running Total in SQL Server
  • Can't find why this datetime test fails, in F#
  • What are the undocumented features and limitations of the…
  • How to convert number to words in java

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to print a date in a regular format?

Next Post:

Make an existing Git branch track a remote branch?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error