如何获取字符的ASCII值 [英] How to get the ASCII value of a character

查看:150
本文介绍了如何获取字符的ASCII值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 ASCII 值,作为int href ="http://en.wikipedia.org/wiki/Python_%28programming_language%29" rel ="noreferrer"> Python ?

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

推荐答案

来自

函数ord()将获得int值 的字符.如果你想 玩完后转换回来 数字,函数chr(​​)可以解决问题.

function ord() would get 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'
>>>

在Python 2中,还有一个unichr函数,返回 Unicode 字符,序数是unichr参数:

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

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

在Python 3中,您可以使用chr代替unichr.

In Python 3 you can use chr instead of unichr.

ord()-Python 3.6.5rc1文档

ord()-Python 2.7.14文档

这篇关于如何获取字符的ASCII值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆