当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python String lower()用法及代码示例


lower()方法将字符串中的所有大写字符转换为小写字符并返回。

用法:

string.lower()

参数:


The lower() method doesn’t take any parameters.

返回值:

returns a lowercased string of the given string

代码1:仅包含字母字符的字符串

# Python3 program to show the 
# working of lower() function 
text = 'GeEks FOR geeKS'
  
print("Original String:") 
print(text) 
  
# lower() function to convert  
# string to lower_case 
print("\nConverted String:") 
print(text.lower())

输出:

Original String:
GeEks FOR geeKS

Converted string:
geeks for geeks

代码2:带有字母数字字符的字符串

# Python3 program to show the 
# working of lower() function 
text = 'G3Ek5 F0R gE3K5'
  
print("Original String:") 
print(text) 
  
# lower() function to convert  
# string to lower_case 
print("\nConverted String:") 
print(text.lower())

输出:

Original String:
G3Ek5 F0R gE3K5

Converted String:
g3ek5 f0r ge3k5

应用:lower()方法的常见应用之一是检查两个字符串是否相同

# Python3 program to show the 
# working of lower() function 
text1 = 'GEEKS For GEEKS'
  
text2 = 'gEeKS fOR GeeKs'
  
# Comparison of strings using  
# lower() method 
if(text1.lower() == text2.lower()): 
    print("Strings are same") 
else:  
    print("Strings are not same")

输出:

Strings are same


相关用法


注:本文由纯净天空筛选整理自Akanksha_Rai大神的英文原创作品 Python | String lower()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。