3

This is my code:

import os
import html

a = html.unescape("home - study")
b = "test"
print(a)
s = (a, b)
print(s)

And this is my result:

home - study
('home\xa0-\xa0study', 'test')

Why does the result print like this?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
E.Tarrent
  • 89
  • 6

1 Answers1

2

By default, printing containers like tuples, lists and others will use the repr of their items. (In CPython, it was chosen to not implement <container>.__str__ and instead let object.__str__ fill its slot. The __str__ of object will then call tuple.__repr__ which then proceeds to call the repr of the elements it contains. See PEP 3140 for more.)

Calling the repr for a string with escape codes (such as \xa0) will, in effect, not escape them:

print(repr(a))
'home\xa0-\xa0study'

To further verify, try print(s[0]). By providing the str object in position 0 directly, python will invoke its __str__ and escape the hex correctly.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • Thank you your explain.I understand the reason now. – E.Tarrent Oct 09 '16 at 03:57
  • 1
    This answer seems to imply that the behaviour is only due to an implementation detail... this is not the case. It is expected that sequences define a `__str__` method in which the representation of the inner elements is obtained calling their `__repr__`. The implementation detail is that they decided to make `__str__` be the same of `__repr__` and avoided defining `__str__` explicitly since `object.__str__` simply calls `__repr__`. There was [a PEP](https://www.python.org/dev/peps/pep-3140/) to try to change this and it was rejected. – Bakuriu Oct 09 '16 at 09:32
  • 1
    @Bakuriu I can see how that's implied. Fixed the body up a bit to make it clear what evolves implementation details and what doesn't. Hadn't read that PEP, thanks! – Dimitris Fasarakis Hilliard Oct 09 '16 at 19:08