3

Offical Python3 docs say this about passing bytes to the single argument constructor for class str:

Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation (see also the -b command-line option to Python).

Ref: https://docs.python.org/3/library/stdtypes.html#str

informal string representation -> Huh?

Using the Python console (REPL), and I see the following weirdness:

>>> ''
''
>>> b''
b''
>>> str()
''
>>> str('')
''
>>> str(b'')
"b''"  # What the heck is this?
>>> str(b'abc')
"b'abc'"
>>> "x" + str(b'')
"xb''"  # Woah.

(The question title can be improved -- I'm struggling to find a better one. Please help to clarify.)

kevinarpe
  • 20,319
  • 26
  • 127
  • 154

1 Answers1

1

The concept behind str seems to be that it returns a "nicely printable" string, usually in a human understandable form. The documentation actually uses the phrase "nicely printable":

If neither encoding nor errors is given, str(object) returns object.__str__(), which is the “informal” or nicely printable string representation of object. For string objects, this is the string itself. If object does not have a __str__() method, then str() falls back to returning repr(object).

With that in mind, note that str of a tuple or list produces string versions such as:

>>> str( (1, 2) )
'(1, 2)'
>>> str( [1, 3, 5] )
'[1, 3, 5]'

Python considers the above to be the "nicely printable" form for these objects. With that as background, the following seems a bit more reasonable:

>>> str(b'abc')
"b'abc'"

With no encoding provided, the bytes b'abc' are just bytes, not characters. Thus, str falls back to the "nicely printable" form and the six character string b'abc' is nicely printable.

John1024
  • 109,961
  • 14
  • 137
  • 171