【问题标题】:How can I convert a character to a integer in Python, and viceversa?如何在 Python 中将字符转换为整数,反之亦然?
【发布时间】:2010-10-16 18:59:43
【问题描述】:

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

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

【问题讨论】:

    标签: python integer char type-conversion


    【解决方案1】:

    使用chr()ord()

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

    【讨论】:

      【解决方案2】:
      >>> ord('a')
      97
      >>> chr(97)
      'a'
      

      【讨论】:

      • 您比其他人早 1 分钟发布了这个答案,但仍然错过了 500 多张赞成票...
      • 那是因为后者被选为答案lol
      • 生活就是这样。我也赞成这个,但我怀疑 11 年后他仍然关心。
      • 后者有文档链接)
      【解决方案3】:

      ord 和 chr

      【讨论】:

      • 我最喜欢这个答案的部分是他们无意中写了一行有效的 Python。
      • print ((ord and chr)(0O041))
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-05-10
        • 1970-01-01
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 2012-04-27
        • 1970-01-01
        • 2011-05-27
        相关资源
        最近更新 更多