Python 2.7 - IPython 'raw_input' and appending to a list - adds 'u' before each item

好久不见. 提交于 2019-12-09 07:42:27

Those are just unicode strings, as opposed the byte strings. There is nothing wrong, and the content of the string is not affected. The u'text' is just so that you can tell the difference between byte strings and unicode strings in Python 2 when you look at the repr. If you print the string, you will see no difference. This is reversed in Python 3, where "text" means a unicode string, while b"bytes" means a byte string.

If you really want to coerce them to bytestrings (unlikely), you can do:

def ensure_str(s):
    if isinstance(s, unicode):
        s = s.encode('utf-8')
    return s

s = ensure_str(raw_input("prompt >"))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!