我想將大寫字符串轉換爲小寫字符串。如何在utthon中將utf大寫字符轉換爲小寫
用於實施例中的字符串'LÄCHERLICH'
,通過使用方法
str.lower()
任何幫助轉化爲'lÄcherlich'
?
感謝
乾杯
我想將大寫字符串轉換爲小寫字符串。如何在utthon中將utf大寫字符轉換爲小寫
用於實施例中的字符串'LÄCHERLICH'
,通過使用方法
str.lower()
任何幫助轉化爲'lÄcherlich'
?
感謝
乾杯
哪個Python版本是什麼呢?在Python 3,使用lower()
正確轉換它:
>>> x = 'LÄCHERLICH'
>>> print(x.lower())
lächerlich
使用Python 2,你應該使用Unicode字符串(不要忘記定義在文件的最開始編碼):
# coding: utf-8
x = u'LÄCHERLICH'
print x.lower().encode('utf8')
這不是問題的答案,應該是註釋 – Craicerjack
我正在使用2.7版本 – Shan
這應做到:
# -*- coding: utf-8 -*-
a = 'LÄCHERLICH'
print a.decode('utf8').lower()
decode
將工作,如果你想在u'LÄCHERLICH'
使用lower()
。
對於Python 2.7
的問題是,當你在聲明字符串它採取爲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
什麼版本的Python? – tambre
我正在使用2.7版本 – Shan