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?
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?
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.