python转化成小写 - python首字母小写
如何在Python中将字符串转换为小写 (6)
有没有办法将字符串从大写,甚至是大写部分转换为小写?
例如公里 - >公里。
如何在Python中将字符串转换为小写?
有没有办法将整个用户输入的字符串从大写,甚至是大写部分转换为小写?
例如公里 - >公里
规范Pythonic这样做的方式是
>>> 'Kilometers'.lower()
'kilometers'
但是,如果目的是做不区分大小写的匹配,您应该使用大小写折叠:
>>> 'Kilometers'.casefold()
'kilometers'
原因如下:
>>> "Maße".casefold()
'masse'
>>> "Maße".lower()
'maße'
>>> "MASSE" == "Maße"
False
>>> "MASSE".lower() == "Maße".lower()
False
>>> "MASSE".casefold() == "Maße".casefold()
True
这是Python 3中的一个str方法,但在Python 2中,您需要查看PyICU或py2casefold - 这里有几个解答 。
Unicode Python 3
Python 3将 Unicode作为常规字符串处理:
>>> string = 'Километр'
>>> string
'Километр'
>>> string.lower()
'километр'
Unicode Python 2
但是Python 2并没有将下面的代码粘贴到shell中,而是使用utf-8
将字符串编码为一串字节。
并且lower
不映射任何原生Unicode对象会意识到的更改,因此我们得到相同的字符串。
>>> string = 'Километр'
>>> string
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> string.lower()
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> print string.lower()
Километр
在脚本中,由于预期的编码是不明确的,因此Python将反对非ascii(如Python 2.5和Python 2.4中的警告)字节在字符串中,但没有给出编码。 有关更多信息,请参阅docs和PEP 263中的Unicode操作指南
使用Unicode文字,而不是文字
所以我们需要一个unicode
字符串来处理这个转换,用unicode文字很容易实现:
>>> unicode_literal = u'Километр'
>>> print unicode_literal.lower()
километр
请注意,这些字节与str
字节完全不同 - 转义字符为'\u'
后面跟着2个字节的宽度,或者这些unicode
字母的16位表示形式:
>>> unicode_literal
u'\u041a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> unicode_literal.lower()
u'\u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
现在,如果我们只有str
的形式,我们需要将它转换为unicode
。 Python的Unicode类型是一种通用编码格式,与其他大多数编码advantages ,它具有许多advantages 。 我们可以使用unicode
编码器的unicode
构造函数或str.decode
方法将str
转换为unicode
:
>>> unicode_from_string = unicode(string, 'utf-8') # "encoding" unicode from string
>>> print unicode_from_string.lower()
километр
>>> string_to_unicode = string.decode('utf-8')
>>> print string_to_unicode.lower()
километр
>>> unicode_from_string == string_to_unicode == unicode_literal
True
这两种方法都转换为unicode类型 - 与unicode_literal相同。
最佳实践,使用Unicode
建议您始终使用Unicode格式的文本 。
软件只能在内部使用Unicode字符串,转换为输出中的特定编码。
可以在必要时进行编码
但是,要在str
返回小写字母,请将python字符串再次编码为utf-8
:
>>> print string
Километр
>>> string
'\xd0\x9a\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> string.decode('utf-8')
u'\u041a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> string.decode('utf-8').lower()
u'\u043a\u0438\u043b\u043e\u043c\u0435\u0442\u0440'
>>> string.decode('utf-8').lower().encode('utf-8')
'\xd0\xba\xd0\xb8\xd0\xbb\xd0\xbe\xd0\xbc\xd0\xb5\xd1\x82\xd1\x80'
>>> print string.decode('utf-8').lower().encode('utf-8')
километр
所以在Python 2中,Unicode可以编码为Python字符串,并且Python字符串可以解码为Unicode类型。
下面解释一些基本的python字符串方法##:
降低
字符串lower()方法将字符串中的所有大写字符转换为小写字符并将其返回。
例:
string = "HeLLO PyTHON"
new_string = string.lower()
print string
print new_string
输出:
HeLLO PyTHON
hello python
资本化 :
在Python中,大写()方法将字符串的第一个字符转换为大写(大写)字母。
例:
string = "hello python"
new_string = string.capitalize()
print string
print new_string
输出:
hello python
Hello python
Swapcase :
字符串swapcase()方法将所有大写字符转换为小写字母,将所有小写字符转换为给定字符串的大写字符并返回它。
string = "HEllO PythOn"
new_string = string.swapcase()
print string
print new_string
产量
HEllO PythOn
heLLo pYTHoN
另外,你可以覆盖一些变量:
s = input('UPPER CASE')
lower = s.lower()
如果你这样使用:
s = "Kilometer"
print(s.lower()) - kilometer
print(s) - Kilometer
它会在被调用时工作。
如果你想用map
来对列表中的多个字符串执行这个操作,你将需要使用str.lower
而不仅仅是lower
:
words = ['CATS', 'KITTENS', 'Pirate Cats', 'fluffy felines']
list(map(str.lower, words))
返回
['cats', 'kittens', 'pirate cats', 'fluffy felines']
它会将字符串转换为小写
string = "XYz"
converted = string.lower();
print("The converted lower case is:",converted)
对于Python 2,这对于UTF-8中的非英文单词不起作用。 在这种情况下, decode('utf-8')
可以帮助:
>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр