且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Python中打印列表元素和字符串有不同的结果

更新时间:2022-04-10 22:45:41

这是因为在打印列表的情况下,Python使用 repr(),但是当打印字符串时,它使用 str()。示例:

This is because in the case of printing a list, Python is using repr(), but when printing a string it is using str(). Example:

unicode_str = 'åäö'
unicode_str_list = [unicode_str, unicode_str]
print 'unwrapped:', unicode_str
print 'in list:', unicode_str_list
print 'repr:', repr(unicode_str)
print 'str:', str(unicode_str)

产生:

unwrapped: åäö
in list: ['\xc3\xa5\xc3\xa4\xc3\xb6', '\xc3\xa5\xc3\xa4\xc3\xb6']
repr: '\xc3\xa5\xc3\xa4\xc3\xb6'
str: åäö