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

前端 未结 3 1301
北恋
北恋 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:35

    You get this because lists can contain any number of elements, of mixed types. In the second case, instead of printing unicode strings, you're printing the list itself - which is very different than printing the list contents.

    Since the list can contain anything, you get the u'foo' syntax. If you were using non-unicode strings, you'd see the 'foo' instead of just foo, as well.

提交回复
热议问题