Python strip() Unicode 字符串? [英] Python strip() unicode string?

查看:37
本文介绍了Python strip() Unicode 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 unicode 字符串上使用像 strip() 这样的字符串方法?并且您不能像普通字符串一样访问 unicode 字符串的字符吗?(例如: mystring[0:4] )

解决方案

它照常工作,只要它们实际上是 unicode,而不是 str(注意:每个字符串文字必须前面有u,就像这个例子一样):

<预><代码>>>>a = u"coțofană">>>一种你'co\u021bofan\u0103'>>>[-1]你'\u0103'>>>a2]你'\u021b'>>>[3]你哦>>>a.strip(u'ă')u'co\u021bofan'

How can you use string methods like strip() on a unicode string? and can't you access characters of a unicode string like with oridnary strings? (ex: mystring[0:4] )

解决方案

It's working as usual, as long as they are actually unicode, not str (note: every string literal must be preceded by u, like in this example):

>>> a = u"coțofană"
>>> a
u'co\u021bofan\u0103'
>>> a[-1]
u'\u0103'
>>> a[2]
u'\u021b'
>>> a[3]
u'o'
>>> a.strip(u'ă')
u'co\u021bofan'

这篇关于Python strip() Unicode 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆