python字符串前面的u,还有r
作者:互联网
以u或U开头的字符串表示unicode字符串
如果你想要用非英语写文本,那么你需要有一个支持Unicode的编辑器。(了解一下unicode和ascll码还有utf-8)
u
'你好'
#这里是把‘你好’用Unicode编码
r
'\nabdcd\rds'
#这里加上r表示 把\nabdcd\rds看做原始字符,忽略一切转意字符
unicodestring = u"Hello world"# 将Unicode转化为普通Python字符串:"encode"
utf8string = unicodestring.encode("utf-8")
asciistring = unicodestring.encode("ascii")
isostring = unicodestring.encode("ISO-8859-1")
utf16string = unicodestring.encode("utf-16")
# 将普通Python字符串转化为Unicode:"decode"
plainstring1 = unicode(utf8string, "utf-8")
plainstring2 = unicode(asciistring, "ascii")
plainstring3 = unicode(isostring, "ISO-8859-1")
plainstring4 = unicode(utf16string, "utf-16")
assert plainstring1 == plainstring2 == plainstring3 == plainstring4
标签:utf,Unicode,前面,python,encode,unicode,字符串,unicodestring 来源: https://www.cnblogs.com/tarzen213/p/11848338.html