簡體   English   中英

如何將字符轉換為 Python 中的 integer,反之亦然?

[英]How can I convert a character to a integer in Python, and viceversa?

給定一個字符,我想得到它的ASCII值。

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

使用chr()ord()

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

ord 和 chr

對於長字符串,您可以使用它。

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

不是 OP 的問題,而是給出標題How can I convert a character to a integer in Python

int(num) <==> ord(num) - ord('0')

str(char) <==> ord(char) - ord('a')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM