在本教程中,我們將借助示例了解 Python String upper() 方法。
upper()
方法將字符串中的所有小寫字符轉換為大寫字符並返回。
示例
message = 'python is fun'
# convert message to uppercase
print(message.upper())
# Output: PYTHON IS FUN
用法:
用法:
string.upper()
參數:
upper()
方法不帶任何參數。
返回:
upper()
方法從給定字符串返回大寫字符串。它將所有小寫字符轉換為大寫。
如果不存在小寫字符,則返回原始字符串。
示例 1:將字符串轉換為大寫
# example string
string = "this should be uppercase!"
print(string.upper())
# string with numbers
# all alphabets should be lowercase
string = "Th!s Sh0uLd B3 uPp3rCas3!"
print(string.upper())
輸出
THIS SHOULD BE UPPERCASE! TH!S SH0ULD B3 UPP3RCAS3!
示例 2:upper() 如何在程序中使用?
# first string
firstString = "python is awesome!"
# second string
secondString = "PyThOn Is AwEsOmE!"
if(firstString.upper() == secondString.upper()):
print("The strings are same.")
else:
print("The strings are not same.")
輸出
The strings are same.
注意:如果要轉換為小寫字符串,請使用String lower.你也可以使用String swapcase在小寫到大寫之間交換。
相關用法
- Python String upper()用法及代碼示例
- Python String Center()用法及代碼示例
- Python String decode()用法及代碼示例
- Python String join()用法及代碼示例
- Python String casefold()用法及代碼示例
- Python String isalnum()用法及代碼示例
- Python String rsplit()用法及代碼示例
- Python String startswith()用法及代碼示例
- Python String rpartition()用法及代碼示例
- Python String splitlines()用法及代碼示例
- Python String isprintable()用法及代碼示例
- Python String translate()用法及代碼示例
- Python String title()用法及代碼示例
- Python String replace()用法及代碼示例
- Python String split()用法及代碼示例
- Python String format_map()用法及代碼示例
- Python String zfill()用法及代碼示例
- Python String max()用法及代碼示例
- Python String isspace()用法及代碼示例
- Python String strip()用法及代碼示例
注:本文由純淨天空篩選整理自 Python String upper()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。