后浪云Python教程:python中u是什么类型

后浪云Python教程:python中u是什么类型插图

web信息中常会遇到“\u4f60\u597d”类型的字符。首先’\u‘开头就基本表明是跟unicode编码相关的,“\u”后的16进制字符串是

相应汉字的utf-16编码。python里decode()和encode()为我们提供了解码和编码的方法。其中decode('unicode_escape')能将此种字符

串解码为unicode字符串。

比如:

str1 = '\u4f60\u597d'
print str1.decode('unicode_escape')
你好

推荐学习《Python教程》!

THE END