美文网首页
python中将汉字转化成反斜线u(\u)开头的字符串

python中将汉字转化成反斜线u(\u)开头的字符串

作者: 暂时还没有昵称2222 | 来源:发表于2016-04-25 15:15 被阅读232次

要求:

'张三' 输出这样的字符串 '\u5f20\u4e09'

python2中代码如下:

u'张三'.encode('unicode-escape')

python3中代码如下:

'张三'.encode('unicode-escape')

如果自己去实现

''.join(r'\u{0:04x}'.format(ord(x)) for x in '张三')

相关文章

网友评论

      本文标题:python中将汉字转化成反斜线u(\u)开头的字符串

      本文链接:https://www.haomeiwen.com/subject/cyfqrttx.html