25. Python upper()

发布时间:2023-12-08

Python upper()

更新:2022-07-24 13:06 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?

示例 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.