如何在 Python 中小写字符串?

发布于 2024-11-25 14:27:12 字数 183 浏览 0 评论 0 原文

有没有办法将字符串转换为小写?

"Kilometers"  →  "kilometers"

请参阅如何将字符串更改为大写?了解相反的情况。

Is there a way to convert a string to lowercase?

"Kilometers"  →  "kilometers"

See How to change a string into uppercase? for the opposite.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

小瓶盖 2024-12-02 14:27:12

使用 str.lower()

"Kilometer".lower()

Use str.lower():

"Kilometer".lower()
泅渡 2024-12-02 14:27:12

执行此操作的规范 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()
'километр'

Python 2,纯字符串文字是字节

在 Python 2 中,下面粘贴到 shell 中,使用 utf-8

并且 lower 不会映射 bytes 会意识到的任何更改,因此我们得到相同的字符串。

>>> 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 中发出警告)字节,因为预期的编码将是不明确的。有关详细信息,请参阅 docsPEP 263

使用 Unicode 文字,不是str 文字

因此我们需要一个 unicode 字符串来处理此转换,使用 unicode 字符串文字可以轻松完成,它可以使用 u 前缀消除歧义(并且请注意,u 前缀在 Python 3 中也适用):

>>> unicode_literal = u'Километр'
>>> print(unicode_literal.lower())
километр

请注意,字节与 str 字节完全不同 - 转义字符为 '\u' 后跟 2 字节宽度或 16 位表示这些 unicode 字母:

>>> 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 类型是一种通用编码格式,相对于大多数其他编码,它具有许多优点 。我们可以使用 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类型。

The canonical Pythonic way of doing this is

>>> '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.

Unicode Python 3

Python 3 handles plain string literals as unicode:

>>> string = 'Километр'
>>> string
'Километр'
>>> string.lower()
'километр'

Python 2, plain string literals are bytes

In Python 2, the below, pasted into a shell, encodes the literal as a string of bytes, using utf-8.

And lower doesn't map any changes that bytes would be aware of, so we get the same string.

>>> 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

Use Unicode literals, not str literals

So we need a unicode string to handle this conversion, accomplished easily with a unicode string literal, which disambiguates with a u prefix (and note the u prefix also works in Python 3):

>>> 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:

>>> 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:

>>> 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.

Best Practice, use Unicode

It is recommended that you always work with text in Unicode.

Software should only work with Unicode strings internally, converting to a particular encoding on output.

Can encode back when necessary

However, to get the lowercase back in type str, encode the python string to utf-8 again:

>>> 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.

缘字诀 2024-12-02 14:27:12

对于 Python 2,这不适用于 UTF-8 中的非英语单词。在这种情况下,decode('utf-8') 可以提供帮助:

>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр

With Python 2, this doesn't work for non-English words in UTF-8. In this case decode('utf-8') can help:

>>> s='Километр'
>>> print s.lower()
Километр
>>> print s.decode('utf-8').lower()
километр
脸赞 2024-12-02 14:27:12

另外,您可以覆盖一些变量:

s = input('UPPER CASE')
lower = s.lower()

如果您像这样使用:

s = "Kilometer"
print(s.lower())     - kilometer
print(s)             - Kilometer

它将在调用时起作用。

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.

无人问我粥可暖 2024-12-02 14:27:12

不要尝试这个,完全不推荐,不要这样做:

import string
s='ABCD'
print(''.join([string.ascii_lowercase[string.ascii_uppercase.index(i)] for i in s]))

输出:

abcd

由于还没有人写它,所以你可以使用 swapcase (所以大写字母将变成小写,反之亦然)(并且这个你应该在我刚刚提到的情况下使用(将上限转换为下限,将下限转换为上限):

s='ABCD'
print(s.swapcase())

输出:

abcd

Don't try this, totally un-recommend, don't do this:

import string
s='ABCD'
print(''.join([string.ascii_lowercase[string.ascii_uppercase.index(i)] for i in s]))

Output:

abcd

Since no one wrote it yet you can use swapcase (so uppercase letters will become lowercase, and vice versa) (and this one you should use in cases where i just mentioned (convert upper to lower, lower to upper)):

s='ABCD'
print(s.swapcase())

Output:

abcd
脸赞 2024-12-02 14:27:12

lowercasing

该方法不仅将拉丁字母表中的所有大写字母转换为小写字母,而且还展示了这样的逻辑是如何实现的。您可以在任何在线 Python 沙箱中测试此代码。

def turnIntoLowercase(string):
    
    lowercaseCharacters = ''
    
    abc = ['a','b','c','d','e','f','g','h','i','j','k','l','m', 
           'n','o','p','q','r','s','t','u','v','w','x','y','z',
           'A','B','C','D','E','F','G','H','I','J','K','L','M',
           'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  
    for character in string:
        if character not in abc:
            lowercaseCharacters += character
        elif abc.index(character) <= 25:
            lowercaseCharacters += character
        else: 
            lowercaseCharacters += abc[abc.index(character) - 26]
    return lowercaseCharacters

string = str(input("Enter your string, please: " ))

print(turnIntoLowercase(string = string))

性能检查

现在,让我们输入以下字符串(然后按 Enter)以确保一切按预期工作:

# Enter your string, please: 

"PYTHON 3.11.2, 15TH FeB 2023"

结果:

"python 3.11.2, 15th feb 2023"

lowercasing

This method not only converts all uppercase letters of the Latin alphabet into lowercase ones, but also shows how such logic is implemented. You can test this code in any online Python sandbox.

def turnIntoLowercase(string):
    
    lowercaseCharacters = ''
    
    abc = ['a','b','c','d','e','f','g','h','i','j','k','l','m', 
           'n','o','p','q','r','s','t','u','v','w','x','y','z',
           'A','B','C','D','E','F','G','H','I','J','K','L','M',
           'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  
    for character in string:
        if character not in abc:
            lowercaseCharacters += character
        elif abc.index(character) <= 25:
            lowercaseCharacters += character
        else: 
            lowercaseCharacters += abc[abc.index(character) - 26]
    return lowercaseCharacters

string = str(input("Enter your string, please: " ))

print(turnIntoLowercase(string = string))

Performance check

Now, let's enter the following string (and press Enter) to make sure everything works as intended:

# Enter your string, please: 

"PYTHON 3.11.2, 15TH FeB 2023"

Result:

"python 3.11.2, 15th feb 2023"
小嗷兮 2024-12-02 14:27:12

如果要将字符串列表转换为小写,可以映射 str.lower

list_of_strings = ['CamelCase', 'in', 'Python']
list(map(str.lower, list_of_strings))            # ['camelcase', 'in', 'python']

If you want to convert a list of strings to lowercase, you can map str.lower:

list_of_strings = ['CamelCase', 'in', 'Python']
list(map(str.lower, list_of_strings))            # ['camelcase', 'in', 'python']
会傲 2024-12-02 14:27:12

有几种不同的方法可以实现这一点。

  1. 使用 .lower() 方法
original_string = "UPPERCASE"
lowercase_string = original_string.lower()
print(lowercase_string)  # Output: "uppercase"
  1. 使用 str.lower()
original_string = "UPPERCASE"
lowercase_string = str.lower(original_string)
print(lowercase_string)  # Output: "uppercase"
  1. 使用 str.translate()str.maketrans() 的组合
original_string = "UPPERCASE"
lowercase_string = original_string.translate(str.maketrans(string.ascii_uppercase, string.ascii_lowercase))
print(lowercase_string)  # Output: "uppercase"

There are several different ways in which this can be done.

  1. Using .lower() method
original_string = "UPPERCASE"
lowercase_string = original_string.lower()
print(lowercase_string)  # Output: "uppercase"
  1. Using str.lower()
original_string = "UPPERCASE"
lowercase_string = str.lower(original_string)
print(lowercase_string)  # Output: "uppercase"
  1. Using combination of str.translate() and str.maketrans()
original_string = "UPPERCASE"
lowercase_string = original_string.translate(str.maketrans(string.ascii_uppercase, string.ascii_lowercase))
print(lowercase_string)  # Output: "uppercase"
岁月无声 2024-12-02 14:27:12

将字符串转换为小写的方法有多种。

使用适合您的方法。

1- .lower() 函数。

语法: string.islower()

属性:

  • 无参数: .lower() 方法不带参数。
  • 自动检查:如果在给定字符串中找不到大写字符,则返回原始字符串。
  • 忽略字符串以外的任何内容:它忽略字符串之间的数字、符号、独特的东西等。

示例:(不采用任何参数)

message = 'I LOVE Python'

# convert message to lowercase
print(message.lower())

输出:

我喜欢Python

示例:(忽略数字)

# example string
string = "THIS SHOULD BE LOWERCASE!"
print(string.lower())

# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 L0w3rCas3!"
print(string.lower())

这应该是小写!
th!s 应该 b3 l0w3rcas3!

独特用法:使用您可以比较2个字符串

# first string
firstString = "I AM ALI!"

# second string
secondString = "i aM AlI!"

if(firstString.lower() == secondString.lower()):
    print("The strings are same.")
else:
    print("The strings are not same.")

输出:字符串相同。

2- SwapCase 功能

  • 它将交换整个大小写。
s = 'IAMALI'
print(s.swapcase())

输出:

亚玛利

3- casefold() 函数

  • 更多幂转换: casefold() 方法更强大、更激进,这意味着它将更多字符转换为小写并发现更多匹配项。
s = 'IAmAli'
print(s.casefold())

输出:

亚玛利

希望有帮助。

There are various ways of converting string into lowercase.

use what suits you.

1- .lower() function.

Syntax: string.islower()

Properties:

  • No Arguments: The .lower() method takes no arguments.
  • Checks Automatically: If no uppercase characters found in given string, it returns the original string.
  • Ignores anythings other then then strings: It ignores the numbers, symbols, unique things etc between the strings.

Example: (no arguments taken)

message = 'I LOVE Python'

# convert message to lowercase
print(message.lower())

Output:

i love python

Example: (ignores numbers)

# example string
string = "THIS SHOULD BE LOWERCASE!"
print(string.lower())

# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 L0w3rCas3!"
print(string.lower())

this should be lowercase!
th!s sh0uld b3 l0w3rcas3!

Unique usage: Use You can compare 2 strings

# first string
firstString = "I AM ALI!"

# second string
secondString = "i aM AlI!"

if(firstString.lower() == secondString.lower()):
    print("The strings are same.")
else:
    print("The strings are not same.")

Output: The strings are same.

2- SwapCase Function

  • It will swap the wholecase.

s = 'IAMALI'
print(s.swapcase())

Output:

iamali

3- casefold() function

  • More Power Convertion: the casefold() method is stronger and more aggressive, which means it will turn more characters into lower case and discover more matches.

s = 'IAmAli'
print(s.casefold())

Output:

iamali

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文