要求:
'张三' 输出这样的字符串 '\u5f20\u4e09'
python2中代码如下:
u'张三'.encode('unicode-escape')
python3中代码如下:
'张三'.encode('unicode-escape')
如果自己去实现
''.join(r'\u{0:04x}'.format(ord(x)) for x in '张三')
要求:
'张三' 输出这样的字符串 '\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
网友评论