大写不适用于特殊字符 - Python

Lea*_*yra 3 python string character utf-8 capitalize

我一直试图大量加载字符串,其中一些以utf-8字符开头.问题是,他们没有大写!

mystring = 'lucas'
mystring.capitalize() # returns 'Lucas'

mytring = 'æthelred'
mystring.capitalize() # returns 'æthelred'
Run Code Online (Sandbox Code Playgroud)

与含有''^¨和字符ð,þ等的元音相同我该怎么做才能解决这个问题?

我实际上无法访问字符串,我将它们放在其他地方,在文本文件中......

Kas*_*mvd 5

你省略了u.字符串需要定义为python的unicode!

>>> mytring = u"æthelred"
>>> print mytring.capitalize()
Æthelred
Run Code Online (Sandbox Code Playgroud)

因为python 3字符串是unicode默认情况下你不需要u.

>>> "æthelred".capitalize()
'Æthelred'
Run Code Online (Sandbox Code Playgroud)

  • 这个答案对于python 2.x是正确的.在python 3.x中,默认情况下字符串是unicode,因此Leandro的代码应该可以正常工作 (2认同)