原文: Python Lowercase – How to Use the String lower() Function

字符串是 Python 中的基本部分。lower() 方法是可用于处理字符串的众多方法之一。

在本文中,我们将了解如何使用 Python 中的 lower() 方法将字符串变为小写。

什么是字符串?

字符串是一种可以包含许多不同字符的数据类型,可以写成一系列字符在单引号或双引号之间。

>>> example_string = 'I am a String!'
>>> example_string
'I am a String!'

什么是方法?

方法是可用于特定数据类型的函数。方法可以有参数,也可以没有。

有时你可能想知道一个方法是否存在。在 Python 中,你可以通过使用带有字符串作为参数的 dir() 函数来查看字符串方法的完整列表,如下所示:

>>> dir(example_string)
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

在本文中,你将了解 lower() 方法及其工作原理。

lower() 方法是如何工作的?

lower() 方法是一个字符串方法,它返回一个完全小写的新字符串。如果原始字符串包含大写字母,则在新字符串中这些字母将是小写。任何小写字母或任何不是字母的字符都不会受到影响。

>>> example_string.lower()
'i am a string!'

>>> 'FREECODECAMP'.lower()
'freecodecamp'

使用 lower 方法时要注意什么

lower() 方法做了一件非常简单的事情:它创建一个新字符串,所有大写字母现在都是小写。但是在使用时有几点需要注意。让我们来看看它们。

字符串是不可变的

字符串是不可变的数据类型,这意味着它们无法更改。使用 lower() 方法后,原始字符串将保持不变。

在上面的例子中,lower() 方法作用于 example_string,但没有改变它。检查 example_string 的值仍然显示原始值。

>>> example_string
'I am a String!'

>>> example_string.lower()
'i am a string!'

>>> example_string
'I am a String!'

lower() 方法返回一个新字符串

lower() 返回一个新字符串。如果你想在代码中再次使用它,则需要将其保存在变量中。

>>> new_string = example_string.lower()

>>> new_string
'i am a string!'

字符串区分大小写

字符串区分大小写,因此小写字符串与大写字符串不同。

>>> 'freecodecamp' == 'FREECODECAMP'
False

这在考虑 lower() 方法的用途时很有用。在示例中,你将看到在构建处理字符串的脚本或程序时,此功能如何使 lower() 方法变得有用和必要。

lower() 方法示例:如何检查用户输入是否匹配

让我们编写一个小脚本,询问用户一个问题并等待输入,并就用户的回答给出反馈。

answer = input("What color is the sun? ")
if answer == "yellow":
  print("Correct!")
else:
  print("That is not the correct color!")
sun_color.py 文件

这个脚本问用户一个问题,“What color is the sun?”,然后等待答案。然后它检查答案是否为 “yellow”,如果是,则打印 “Correct!” 如果不是,它会打印 “That is not the correct color!”

但是这个脚本有一个问题。

运行此脚本,你将在终端中询问此问题:

$ python sun_color.py
What color is the sun? 

如果你回答 “Yellow”,它会说:

$ python sun_color.py
What color is the sun? Yellow
That is not the correct color!

为什么会这样?

请记住,字符串区分大小写。该脚本正在检查用户是否输入了 yellow 字符串——Yellow,带有大写的 “Y”,是一个不同的字符串。

你可以通过使用 lower() 方法轻松解决此问题,并对 sun_color.py 文件进行以下小改动:

answer = input("What color is the sun? ")
if answer.lower() == "yellow":
  print("Correct!")
else:
  print("That is not the correct color!")
修改后的 sun_color.py 文件

现在,如果你再试一次...

>>> python sun_color.py
What color is the sun? Yellow
Correct!

发生了什么变化?编写 answer.lower()  确保检查的字符串完全小写,然后再将其与正确的答案字符串 “yellow” 进行比较。这样,用户写 “YELLOW” 或 “yELLOW” 或 “yellow” 都没有关系——它们都被转换为小写。

谢谢阅读!现在你知道如何在 JavaScript 中使用 lower() 方法了。