Python String lower()教程

Python提供了可以使用大小写字母或字符的字符串类型。有不同的方法来处理 下() 方法用于将所有字母变为小写。如果字符是大写的,它将被转换成小写的。如果字符是小写的,则不会有任何更改。

null

lower()方法语法

lower()方法的语法非常简单 返回小写 指定字符串的版本。作为一种内置方法,不需要导入额外的模块。

STRING.lower()
  • 字符串 是将变为小写的字符串变量或字符串文字。

使字符串变为小写

在下面的示例中,我们将字符串变量转换为小写。所有字母都将用lower()方法转换成小写。

sentence1 = "I Like PythonTect"lowercase_sentence1= sentence1.lower()print(lowercase_sentence1)sentence2 = "I Like 33 PythonTect"lowercase_sentence2= sentence2.lower()
print(lowercase_sentence2)

输出如下,因为数字没有小写字母,所以不会更改。

i like pythontecti like 33 pythontect

使字符串文字小写

字符串文字也是一种字符串类型,但区别在于它们不存储在变量中。但是它们也是字符串,但是像lower()这样的字符串方法中的butil也可以通过这些字符串文本调用。

lowercase_sentence1= "I Like PythonTect".lower()
print(lowercase_sentence1)

lowercase_sentence2= "I Like 33 PythonTect".lower()
print(lowercase_sentence2)

输出如下所示,这与字符串变量相同,因为数字没有小写字母,所以不会更改。

i like pythontecti like 33 pythontect

检查给定字符串是否为小写

lower()方法还可以通过使用if..else语句来检查提供的字符串或输入字符串。在下面的示例中,我们将使用lower()方法降低所有给定的句子,并检查它们是否相同。我们还可以使用字符串文字将其变为小写,然后进行检查。

sentence1 = "I Like PythonTect"
sentence2 = "I LIKE PYTHONTECT"
if(sentence1.lower() == sentence2.lower()):   print("The sentences are the same")else:   print("The sentences are NOT the same")if(sentence1.lower() == "I LIKE PYTHONTECT".lower()):
   print("The sentences are the same")
else:
   print("The sentences are NOT the same")

输出如下。

The sentences are the sameThe sentences are the same
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享