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


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


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。