Convert string to ASCII value python

后端 未结 8 1718
深忆病人
深忆病人 2020-12-07 22:43

How would you convert a string to ASCII values?

For example, \"hi\" would return 104105.

I can individually do ord(\'h\') and ord(\'i\'), but it\'s going to

8条回答
  •  旧时难觅i
    2020-12-07 23:07

    Here is a pretty concise way to perform the concatenation:

    >>> s = "hello world"
    >>> ''.join(str(ord(c)) for c in s)
    '10410110810811132119111114108100'
    

    And a sort of fun alternative:

    >>> '%d'*len(s) % tuple(map(ord, s))
    '10410110810811132119111114108100'
    

提交回复
热议问题