Why do I get the u“xyz” format when I print a list of unicode strings in Python?

前端 未结 3 1312
北恋
北恋 2020-12-18 16:31

Please observe the following behavior:

a = u\"foo\"
b = u\"b\\xe1r\"   # \\xe1 is an \'a\' with an accent
s = [a, b]

print a, b
print s
for x in s: print x,         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-18 16:36

    When you print a list, you get the repr() of each element, lists aren't really meant to be printed, so python tries to print something representative of it's structure.

    If you want to format it in any particular way, either be explicit about how you want it formatted, or override it's __repr__ method.

提交回复
热议问题