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

标签 python string utf-8 character capitalize

我一直在尝试将大量字符串大写,其中一些以 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/

相关文章:

python - TCP套接字函数recv()阻塞程序

python - Python string.find 使用什么?

c++ - 在 C++ 中使用 utf8 库将 UTF 16 转换为 UTF8

Python 用括号分割字符串

python - 从 Python 列表列表到整数索引的逆映射

c# - 如何将字符串数组转为int数组进行计算?

string - 如何在字符串中添加换行符?

vim - vim 或其他地方文件编码和编码的基本原理

php - Zend_Mail 中的 UTF-8 发件人姓名?

python - 创建 super 用户时出现 Django 错误,AttributeError : 'Manager' object has no attribute 'get_by_natural_key'