當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。