Search code examples
pythonpython-3.xtrimremoving-whitespace

Do not strip White spaces at the beginning or end of a text - Python


I have this code in Python that converts a decimal to its corresponding ASCII character.

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

The output should be "What is a Creel?" not "What is aCreel" . How will I keep the blank spaces at the beginning or end of my text?


Solution

  • Your input lacks the space at the end:

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