ChatGPT解决这个技术问题 Extra ChatGPT

如何在 Python 中将字符转换为整数,反之亦然?

给定一个字符,我想获得它的 ASCII 值。

例如,对于字符 a,我想得到 97,反之亦然。


A
Adam Rosenfield

使用 chr()ord()

>>> chr(97)
'a'
>>> ord('a')
97

d
dwc
>>> ord('a')
97
>>> chr(97)
'a'

您比其他人早 1 分钟发布了这个答案,但仍然错过了 500 多张赞成票……
那是因为后者被选为答案哈哈
这就是生活。我也赞成这个,但我怀疑他在 11 年后仍然在乎。
后者有文档链接)
r
rmmh

ord 和 chr


关于这个答案,我最喜欢的部分是他们无意中写了一行有效的 Python。
print ((ord and chr)(0O041))
P
Pjl

对于长字符串,您可以使用它。

 ''.join(map(str, map(ord, 'pantente')))