pythonpython-bytearray

python string decode displayed as byte array in list context


Why is this string printing as a byte array in the context of a list, printing as expected in a print statement, and the type is of string, not bytearray?

stringList = []
# Comparing a string, and a string decoded from a byte array
theString = "Hello World1"
value = byteArray.decode("utf-8") # byteArray is set externally, but prints correctly below

# Types are the same
print("theString type: " + str(type(theString)))
print("value type: " + str(type(value)))

# Value are displayed the same
print("theString: " + theString)
print("value: " + value)

# Add each to list
stringList.append(theString)
stringList.append(value)

# the value string prints as a byte array
print(stringList)

Output:

theString type: <class 'str'>
value type: <class 'str'>
theString: Hello World1
value: Hello World0
['Hello World1', 'H\x00\x00\x00e\x00\x00\x00l\x00\x00\x00l\x00\x00\x00o\x00\x00\x00\x00\x00\x00W\x00\x00\x00o\x00\x00\x00r\x00\x00\x00l\x00\x00\x00d\x00\x00\x000\x00\x00\x00']

Solution

  • The way python prints things when you do:

    print("value: " + value)
    

    and

    print(["Hello", value])
    

    is quite different.

    The first approach is printing the characters of the string on the console. While the second is not - it is showing the repr of value.

    For example:

    >>> value = 'H\x00e\x00llo'
    
    >>> print(repr(value))
    'H\x00e\x00llo'
    
    >>> print(str(value))             # hidden characters in a string are well ... hidden
    Hello
    
    >>> print("value: " + value)      # Adding 2 strings gives a string
    value: Hello
    
    >>> print("value:", value)        # value is a string - so hidden characters are not printed
    value: Hello
    
    >>> print("value:", repr(value))  # the repr still shows us the hidden characters
    value: 'H\x00e\x00llo'
    
    >>> print(['Hello', value])       # list uses repr
    ['Hello', 'H\x00e\x00llo']