不要在文本的开头或结尾处去除空格-Python

时间:2018-11-05 10:27:05

标签: python python-3.x trim removing-whitespace

我在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?')

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

1 个答案:

答案 0 :(得分:1)

您输入的内容末尾没有空格:

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