How to get the ASCII value of a character

前端 未结 6 501
天命终不由人
天命终不由人 2020-11-22 07:04

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

6条回答
  •  难免孤独
    2020-11-22 07:28

    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
    

提交回复
热议问题