upper()方法將字符串中的所有小寫字符轉換為大寫字符並返回
用法:
string.upper()
參數:
The
upper()
method doesn’t take any parameters.
返回值:
returns a uppercased string of the given string
代碼1:僅包含字母字符的字符串
# Python3 program to show the
# working of upper() function
text = 'geeKs For geEkS'
print("Original String:")
print(text)
# upper() function to convert
# string to upper_case
print("\nConverted String:")
print(text.upper())
輸出:
Original String: geeKs For geEkS Converted String: GEEKS FOR GEEKS
代碼2:帶有字母數字字符的字符串
# Python3 program to show the
# working of upper() function
text = 'g3Ek5 f0r gE3K5'
print("Original String:")
print(text)
# upper() function to convert
# string to upper_case
print("\nConverted String:")
print(text.upper())
輸出:
Original String: g3Ek5 f0r gE3K5 Converted String: G3EK5 F0R GE3K5
應用:upper()方法的常見應用之一是檢查兩個字符串是否相同
# Python3 program to show the
# working of upper() function
text1 = 'geeks fOr geeks'
text2 = 'gEeKS fOR GeeKs'
# Comparison of strings using
# upper() method
if(text1.upper() == text2.upper()):
print("Strings are same")
else:
print("Strings are not same")
輸出:
Strings are same
相關用法
注:本文由純淨天空篩選整理自Akanksha_Rai大神的英文原創作品 Python | String upper()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。