Search code examples
pythonconcatenationascii

python concatenating \x00 as string


I am trying to concatenate multiple values in a list, all of the string datatype.

The issue is whenever I encounter the return string \x00, even when it is stored as a string "\x00", the code stops doing whatever it does and finishes.

Below is the code:

example = [5, "HELLO", "\x00", "abc", "\x00", 5]
print(example)

test = []
for value in example:
    test.append(str(value))
print("".join(test))

and here is the output:

[5, 'HELLO', '\x00', 'abc', '\x00', 5]
5HELLO

as you can see, after "HELLO", everything after is not included.Is there a way to get around this?

Apologies for any misunderstandings that i may have, I am still learning this language.


Solution

  • The printing of null characters in strings depends on the terminal/program being used, it might well be that your program/IDE stops printing a string when it encounters a null character, while other programs might print whitespaces or skip them. A similar question and answer can be found here: Printing Null Character ("\x00") in Python vs C