python 列表中的“u”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1709110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does 'u' mean in a list?
提问by Federer
This is the first time I've came across this. Just printed a list and each element seems to have a u
in front of it i.e.
这是我第一次遇到这种情况。刚刚打印了一个列表,每个元素u
前面似乎都有一个,即
[u'hello', u'hi', u'hey']
What does it mean and why would a list have this in front of each element?
这是什么意思,为什么列表在每个元素前面都有这个?
As I don't know how common this is, if you'd like to see how I came across it, I'll happily edit the post.
由于我不知道这有多常见,如果你想看看我是如何遇到它的,我很乐意编辑这篇文章。
回答by SilentGhost
it's an indication of unicode string. similar to r''
for raw string.
它是 unicode 字符串的指示。类似于r''
原始字符串。
>>> type(u'abc')
<type 'unicode'>
>>> r'ab\c'
'ab\c'
回答by Rasmus Kaj
The u
just means that the following string is a unicode string (as opposed to a plain ascii string). It has nothing to do with the list that happens to contain the (unicode) strings.
的u
只是意味着下面的字符串是Unicode字符串(相对于纯ASCII字符串)。它与恰好包含(unicode)字符串的列表无关。
回答by toasteroven
I believe the u' prefix creates a unicode string instead of regular ascii
我相信 u' 前缀会创建一个 unicode 字符串而不是常规的 ascii