[issue42852] pprint fails in transformming non-breaking space

Steven D'Aprano report at bugs.python.org
Thu Jan 7 06:59:56 EST 2021


Steven D'Aprano <steve+python at pearwood.info> added the comment:

By the way, there is no need to use the u'' prefix on strings in Python 3.

Unfortunately, I think this is a case where pprint doesn't meet your expectations. pprint is designed to print the repr of a string, while regular print prints the human-visible str:

>>> print(repr('\240 hello'))
'\xa0 hello'
>>> print('\240 hello')
  hello

Its not just non-breaking space, it also includes ASCII characters:


>>> print(repr('\01 hello'))
'\x01 hello'
>>> print('\01 hello')
 hello
>>> pprint.pprint('\01 hello')
'\x01 hello'


So this is intentional behaviour, not a bug. If you want to change the behaviour, it will probably require a re-design of the way strings are printed by the pprint module.

----------
nosy: +steven.daprano

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42852>
_______________________________________


More information about the Python-bugs-list mailing list