Is there a way to convert a string from uppercase, or even part uppercase to lowercase?
是否有一种方法可以将字符串从大写转换为小写,甚至将大写的部分转换为小写?
E.g. Kilometers --> kilometers.
例如公里- - >公里。
9 个解决方案
#1
#2
132
This doesn't work for non-English words in UTF-8. In this case decode('utf-8')
can help:
这对UTF-8的非英语单词不适用。在这种情况下,decode(“utf-8”)可以提供帮助:
>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр
#3
85
How to convert string to lowercase in Python?
Is there any way to convert an entire user inputted string from uppercase, or even part uppercase to lowercase?
是否有任何方法可以将整个用户输入的字符串从大写,甚至是部分大写转换为小写?
E.g. Kilometers --> kilometers
例如公里- - >公里
The canonical Pythonic way of doing this is
规范的python方法是。
>>> 'Kilometers'.lower()
'kilometers'
However, if the purpose is to do case insensitive matching, you should use case-folding:
但是,如果目的是做不区分大小写的匹配,则应该使用案例折叠:
>>> 'Kilometers'.casefold()
'kilometers'
Here's why:
原因如下:
>>> "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
This is a str method in Python 3, but in Python 2, you'll want to look at the PyICU or py2casefold - several answers address this here.
这是Python 3中的一个str方法,但是在Python 2中,您将希望看到PyICU或py2casefold—几个答案在这里解决这个问题。
Unicode Python 3
Python 3 handles unicode as regular strings:
Python 3处理unicode作为常规字符串:
>>> string = 'Километр'
>>> string
'Километр'
>>> string.lower()
'километр'
Unicode Python 2
But Python 2 does not, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8
.
但是Python 2没有,下面,粘贴到一个shell中,使用utf-8将文字编码成字符串。
And lower
doesn't map any changes that native Unicode objects would be aware of, so we get the same string.
而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()
Километр
In scripts, Python will object to non-ascii (as of Python 2.5, and warning in Python 2.4) bytes being in a string with no encoding given, since the intended coding would be ambiguous. For more on that, see the Unicode how-to in the docs and PEP 263
在脚本中,Python会反对非ascii(如Python 2.5,并且在Python 2.4中警告)在没有编码的字符串中存在,因为预期的编码将是不明确的。有关这方面的更多信息,请参阅文档和PEP 263中的Unicode指南。
Use Unicode literals, not str
literals
So we need a unicode
string to handle this conversion, accomplished easily with a unicode literal:
因此,我们需要一个unicode字符串来处理这种转换,可以轻松地使用unicode字符来完成:
>>> unicode_literal = u'Километр'
>>> print unicode_literal.lower()
километр
Note that the bytes are completely different from the str
bytes - the escape character is '\u'
followed by the 2-byte width, or 16 bit representation of these unicode
letters:
注意,字节与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'
Now if we only have it in the form of a str
, we need to convert it to unicode
. Python's Unicode type is a universal encoding format that has many advantages relative to most other encodings. We can either use the unicode
constructor or str.decode
method with the codec to convert the str
to unicode
:
现在,如果我们只使用str的形式,我们需要将其转换为unicode。Python的Unicode类型是一种通用的编码格式,与大多数其他编码相比,它有许多优点。我们可以使用unicode构造函数或string .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
Both methods convert to the unicode type - and same as the unicode_literal.
这两种方法都转换为unicode类型,与unicode_literal相同。
Best Practice, use Unicode
It is recommended that you always work with text in Unicode.
建议您总是使用Unicode的文本。
Software should only work with Unicode strings internally, converting to a particular encoding on output.
软件应该只在内部使用Unicode字符串,转换成特定的输出编码。
Can encode back when necessary
However, to get the lowercase back in type str
, encode the python string to utf-8
again:
但是,要在类型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')
километр
So in Python 2, Unicode can encode into Python strings, and Python strings can decode into the Unicode type.
因此在Python 2中,Unicode可以编码到Python字符串中,而Python字符串可以解码为Unicode类型。
#4
73
You can do what Peter said, or if you want the user to input something you could do this:
你可以按照Peter说的做,或者如果你想让用户输入你可以做的事情:
raw_input('Type Something').lower()
It will then automatically convert the thing they typed into lowercase.
然后它会自动将他们键入的东西转换成小写。
Note: raw_input
was renamed to input
in Python 3.x and above.
注意:raw_input被重命名为Python 3中的输入。x和。
#5
20
Also, you can overwrite some variables:
同样,你也可以改写一些变量:
s = input('UPPER CASE')
lower = s.lower()
If you use like this:
如果你这样使用:
s = "Kilometer"
print(s.lower()) - kilometer
print(s) - Kilometer
It will work just when called.
它将在调用时工作。
#6
3
If you want to use map
to perform this operation on multiple strings in a list you will need to use str.lower
and not merely lower
:
如果您想要使用map在列表中多个字符串上执行此操作,您将需要使用str.lower,而不仅仅是更低的:
words = ['CATS', 'KITTENS', 'Pirate Cats', 'fluffy felines']
list(map(str.lower, words))
Returns
返回
['cats', 'kittens', 'pirate cats', 'fluffy felines']
#7
2
If the whole text is uppercase like "KILOMETER", and you only want the first character to be lowercased, then do
如果整个文本是大写的“公里”,而您只希望第一个字符是小写的,那么就这样做。
text = "KILOMETER"
result = text[:1] + text[1:].lower()
print(result)
But to lowercase the whole string, do
但是对于小写的整个字符串,可以。
text = "KILOMETER"
text = text.lower()
print(text)
#8
0
Some basic python string methods are explained below ##:
Lower
The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.
string lower()方法将字符串中的所有大写字符转换为小写字符并返回它。
Example:
例子:
string = "HeLLO PyTHON"
new_string = string.lower()
print string
print new_string
Output:
输出:
HeLLO PyTHON
hello python
Capitalize:
In Python, the capitalize() method converts the first character of a string to capital (uppercase) letter.
在Python中,大写()方法将字符串的第一个字符转换为大写(大写)字母。
Example:
例子:
string = "hello python"
new_string = string.capitalize()
print string
print new_string
Output:
输出:
hello python
Hello python
Swapcase:
The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string and returns it.
string swapcase()方法将所有大写字符转换为小写字符,并将所有小写字符转换为给定字符串的大写字符并返回它。
string = "HEllO PythOn"
new_string = string.swapcase()
print string
print new_string
Output
输出
HEllO PythOn
heLLo pYTHoN
#9
0
It will convert the string to lower case
它将把字符串转换为小写字母。
string = "XYz"
converted = string.lower();
print("The converted lower case is:",converted)
#1
#2
132
This doesn't work for non-English words in UTF-8. In this case decode('utf-8')
can help:
这对UTF-8的非英语单词不适用。在这种情况下,decode(“utf-8”)可以提供帮助:
>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр
#3
85
How to convert string to lowercase in Python?
Is there any way to convert an entire user inputted string from uppercase, or even part uppercase to lowercase?
是否有任何方法可以将整个用户输入的字符串从大写,甚至是部分大写转换为小写?
E.g. Kilometers --> kilometers
例如公里- - >公里
The canonical Pythonic way of doing this is
规范的python方法是。
>>> 'Kilometers'.lower()
'kilometers'
However, if the purpose is to do case insensitive matching, you should use case-folding:
但是,如果目的是做不区分大小写的匹配,则应该使用案例折叠:
>>> 'Kilometers'.casefold()
'kilometers'
Here's why:
原因如下:
>>> "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
This is a str method in Python 3, but in Python 2, you'll want to look at the PyICU or py2casefold - several answers address this here.
这是Python 3中的一个str方法,但是在Python 2中,您将希望看到PyICU或py2casefold—几个答案在这里解决这个问题。
Unicode Python 3
Python 3 handles unicode as regular strings:
Python 3处理unicode作为常规字符串:
>>> string = 'Километр'
>>> string
'Километр'
>>> string.lower()
'километр'
Unicode Python 2
But Python 2 does not, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8
.
但是Python 2没有,下面,粘贴到一个shell中,使用utf-8将文字编码成字符串。
And lower
doesn't map any changes that native Unicode objects would be aware of, so we get the same string.
而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()
Километр
In scripts, Python will object to non-ascii (as of Python 2.5, and warning in Python 2.4) bytes being in a string with no encoding given, since the intended coding would be ambiguous. For more on that, see the Unicode how-to in the docs and PEP 263
在脚本中,Python会反对非ascii(如Python 2.5,并且在Python 2.4中警告)在没有编码的字符串中存在,因为预期的编码将是不明确的。有关这方面的更多信息,请参阅文档和PEP 263中的Unicode指南。
Use Unicode literals, not str
literals
So we need a unicode
string to handle this conversion, accomplished easily with a unicode literal:
因此,我们需要一个unicode字符串来处理这种转换,可以轻松地使用unicode字符来完成:
>>> unicode_literal = u'Километр'
>>> print unicode_literal.lower()
километр
Note that the bytes are completely different from the str
bytes - the escape character is '\u'
followed by the 2-byte width, or 16 bit representation of these unicode
letters:
注意,字节与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'
Now if we only have it in the form of a str
, we need to convert it to unicode
. Python's Unicode type is a universal encoding format that has many advantages relative to most other encodings. We can either use the unicode
constructor or str.decode
method with the codec to convert the str
to unicode
:
现在,如果我们只使用str的形式,我们需要将其转换为unicode。Python的Unicode类型是一种通用的编码格式,与大多数其他编码相比,它有许多优点。我们可以使用unicode构造函数或string .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
Both methods convert to the unicode type - and same as the unicode_literal.
这两种方法都转换为unicode类型,与unicode_literal相同。
Best Practice, use Unicode
It is recommended that you always work with text in Unicode.
建议您总是使用Unicode的文本。
Software should only work with Unicode strings internally, converting to a particular encoding on output.
软件应该只在内部使用Unicode字符串,转换成特定的输出编码。
Can encode back when necessary
However, to get the lowercase back in type str
, encode the python string to utf-8
again:
但是,要在类型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')
километр
So in Python 2, Unicode can encode into Python strings, and Python strings can decode into the Unicode type.
因此在Python 2中,Unicode可以编码到Python字符串中,而Python字符串可以解码为Unicode类型。
#4
73
You can do what Peter said, or if you want the user to input something you could do this:
你可以按照Peter说的做,或者如果你想让用户输入你可以做的事情:
raw_input('Type Something').lower()
It will then automatically convert the thing they typed into lowercase.
然后它会自动将他们键入的东西转换成小写。
Note: raw_input
was renamed to input
in Python 3.x and above.
注意:raw_input被重命名为Python 3中的输入。x和。
#5
20
Also, you can overwrite some variables:
同样,你也可以改写一些变量:
s = input('UPPER CASE')
lower = s.lower()
If you use like this:
如果你这样使用:
s = "Kilometer"
print(s.lower()) - kilometer
print(s) - Kilometer
It will work just when called.
它将在调用时工作。
#6
3
If you want to use map
to perform this operation on multiple strings in a list you will need to use str.lower
and not merely lower
:
如果您想要使用map在列表中多个字符串上执行此操作,您将需要使用str.lower,而不仅仅是更低的:
words = ['CATS', 'KITTENS', 'Pirate Cats', 'fluffy felines']
list(map(str.lower, words))
Returns
返回
['cats', 'kittens', 'pirate cats', 'fluffy felines']
#7
2
If the whole text is uppercase like "KILOMETER", and you only want the first character to be lowercased, then do
如果整个文本是大写的“公里”,而您只希望第一个字符是小写的,那么就这样做。
text = "KILOMETER"
result = text[:1] + text[1:].lower()
print(result)
But to lowercase the whole string, do
但是对于小写的整个字符串,可以。
text = "KILOMETER"
text = text.lower()
print(text)
#8
0
Some basic python string methods are explained below ##:
Lower
The string lower() method converts all uppercase characters in a string into lowercase characters and returns it.
string lower()方法将字符串中的所有大写字符转换为小写字符并返回它。
Example:
例子:
string = "HeLLO PyTHON"
new_string = string.lower()
print string
print new_string
Output:
输出:
HeLLO PyTHON
hello python
Capitalize:
In Python, the capitalize() method converts the first character of a string to capital (uppercase) letter.
在Python中,大写()方法将字符串的第一个字符转换为大写(大写)字母。
Example:
例子:
string = "hello python"
new_string = string.capitalize()
print string
print new_string
Output:
输出:
hello python
Hello python
Swapcase:
The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string and returns it.
string swapcase()方法将所有大写字符转换为小写字符,并将所有小写字符转换为给定字符串的大写字符并返回它。
string = "HEllO PythOn"
new_string = string.swapcase()
print string
print new_string
Output
输出
HEllO PythOn
heLLo pYTHoN
#9
0
It will convert the string to lower case
它将把字符串转换为小写字母。
string = "XYz"
converted = string.lower();
print("The converted lower case is:",converted)