我一直在尝试将大量字符串大写,其中一些以 utf-8 字符开头。 问题是,他们没有大写!
mystring = 'lucas'
mystring.capitalize() # returns 'Lucas'
mytring = 'æthelred'
mystring.capitalize() # returns 'æthelred'
与包含 `´^¨ 和字符 ð、þ 等的元音相同。 我该怎么做才能解决这个问题?
我实际上无权访问该字符串,我在其他地方获取它们,在一个文本文件中...
最佳答案
你省略了 u
。需要将字符串定义为 python 的 unicode!
>>> mytring = u"æthelred"
>>> print mytring.capitalize()
Æthelred
在 python 3
中字符串默认是 unicode 你不需要 u
。
>>> "æthelred".capitalize()
'Æthelred'
关于python - 大写不适用于特殊字符 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723685/