首页 > 解决方案 > 不要在文本的开头或结尾去除空格 - Python

问题描述

我在 Python 中有这段代码,可以将十进制转换为其相应的 ASCII 字符。

import codecs
def convert_decimal_to_string(decrypted):
    decrypted = hex(decrypted)
    decrypted = decrypted.replace('0x', '')        
    return codecs.decode(codecs.decode(decrypted,'hex'),'ascii')

decrypted = 1612388154947973357665
decrypted = convert_decimal_to_string(decrypted)
print(decrypted + 'Creel?')

输出应该是“什么是纱架?” 不是“什么是纱架”。如何在文本的开头或结尾保留空格?

标签: pythonpython-3.xtrimremoving-whitespace

解决方案


您的输入最后缺少空间:

>>> hex(1612388154947973357665)
'0x576861742069732061'
>>> # manually add \x for every pair of digits:
>>> '\x57\x68\x61\x74\x20\x69\x73\x20\x61'
'What is a'

推荐阅读