如何在python中将UTF大写字符转换为小写字符
我想将大写字符串转换为小写字符串 例如,字符串如何在python中将UTF大写字符转换为小写字符,python,string,python-2.7,unicode,python-unicode,Python,String,Python 2.7,Unicode,Python Unicode,我想将大写字符串转换为小写字符串 例如,字符串'LÄCHERLICH'通过使用以下方法转换为'LÄCHERLICH' str.lower() 它是哪个Python版本?在Python 3中,使用lower() >>> x = 'LÄCHERLICH' >>> print(x.lower()) lächerlich 对于Python 2,您应该使用unicode字符串(不要忘记在文件的开头定义编码): 这应该做到: # -*- coding: utf-8
'LÄCHERLICH'
通过使用以下方法转换为'LÄCHERLICH'
str.lower()
它是哪个Python版本?在Python 3中,使用
lower()
>>> x = 'LÄCHERLICH'
>>> print(x.lower())
lächerlich
对于Python 2,您应该使用unicode字符串(不要忘记在文件的开头定义编码):
这应该做到:
# -*- coding: utf-8 -*-
a = 'LÄCHERLICH'
print a.decode('utf8').lower()
decode
的工作方式就像您想在Python 2.7的u'LÄCHERLICH'
上使用lower()
一样
问题是,当您将字符串声明为ascii时,您必须在声明时或声明之后用UTF定义它
In [17]: str = 'LÄCHERLICH' # didn't specify encoding(so ASCII by default)
In [18]: print str.lower()
lÄcherlich
In [19]: str = u'LÄCHERLICH' #declaring that it's UTF
In [20]: print str.lower()
lächerlich
声明后转换:
In [21]: str = 'LÄCHERLICH'
In [22]: print str.decode('utf8').lower()
lächerlich
我使用的是2.7版
In [21]: str = 'LÄCHERLICH'
In [22]: print str.decode('utf8').lower()
lächerlich