简体   繁体   English

Python strip()unicode字符串?

[英]Python strip() unicode string?

How can you use string methods like strip() on a unicode string? 如何在unicode字符串上使用strip()之类的字符串方法? and can't you access characters of a unicode string like with oridnary strings? 并且你不能访问像oridnary字符串一样的unicode字符串的字符? (ex: mystring[0:4] ) (例如: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): 它正常工作,只要它们实际上是unicode ,而不是str (注意:每个字符串文字都必须u ,就像在这个例子中一样):

>>> 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'

Maybe it's a bit late to answer to this, but if you are looking for the library function and not the instance method, you can use that as well. 也许回答这个问题有点晚了,但是如果你正在寻找库函数而不是实例方法,你也可以使用它。 Just use: 只需使用:

yourunicodestring = u'  a unicode string with spaces all around  '
unicode.strip(yourunicodestring)

In some cases it's easier to use this one, for example inside a map function like: 在某些情况下,使用这个更容易,例如在地图函数中:

unicodelist=[u'a',u'   a   ',u' foo is just...foo   ']
map (unicode.strip,unicodelist)

You can do every string operation, actually in Python 3, all str's are unicode. 您可以执行每个字符串操作,实际上在Python 3中,所有str都是unicode。

>>> my_unicode_string = u"abcşiüğ"
>>> my_unicode_string[4]
u'i'
>>> my_unicode_string[3]
u'\u015f'
>>> print(my_unicode_string[3])
ş
>>> my_unicode_string[3:]
u'\u015fi\xfc\u011f'
>>> print(my_unicode_string[3:])
şiüğ
>>> print(my_unicode_string.strip(u"ğ"))
abcşiü

See the Python docs on Unicode strings and the following section on string methods. 请参阅有关Unicode字符串Python文档以及有关字符串方法的以下部分。 Unicode strings support all of the usual methods and operations as normal ASCII strings. Unicode字符串支持所有常用的方法和操作,如普通的ASCII字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM