Tutorial Study Image

Python upper()

Python 中的 upper() 函数用于返回原始字符串的副本,其中字符串中所有的小写字符都被转换为大写。如果没有小写字母,它将返回原始字符串。


string.upper() 
 

upper() 参数

upper() 方法不接受任何参数。在转换时,所有符号和字母都会被忽略。

upper() 返回值

返回值始终是一个字符串。此方法类似于 capitalize() 方法,不同之处在于 capitalize() 只将单词的首字母转换为大写,而 upper() 将字符串的所有字符转换为大写。

输入 返回值
字符串 大写字符串

Python 中 upper() 方法的示例

示例 1:如何将字符串转换为大写


# example string
string = "How are you?"
print(string.upper())

# string with numbers

string = "Hii! How Are You123?"
print(string.lower())
 

输出


HOW ARE YOU?
HII! HOW ARE YOU123?

示例 2:upper() 如何在程序中与条件检查一起使用?


# first string
String1 = "ProgRammIng LanGuaGe!"

# second string
String2 = "programming language!"

if(String1.upper() == String2.upper()):
    print("The two strings are same.")
else:
    print("The two strings are not same.")
 

输出


The two strings are same.