Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将UTF大写字符转换为小写字符_Python_String_Python 2.7_Unicode_Python Unicode - Fatal编程技术网

如何在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