如何用Python中的lower() 函数使字符串小写

147 阅读4分钟

字符串是使用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() 方法已经作用于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!")

the sun_color.py 文件

这个脚本问用户一个问题:"太阳是什么颜色的?",并等待用户的回答。然后,它检查答案是否是 "黄色",如果是,它就打印 "正确!"如果不是,则打印 "这不是正确的颜色!"。

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

运行这个脚本,你会在终端中被问到这个问题。

$ python sun_color.py
What color is the sun? 

如果你回答 "黄色",它会说:

$ 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 "就不重要了--它们都被转换为小写。

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