2023年6月10日 星期六

[Python] 字串的大小寫處理函數

upper() 函數

upper() 函數用於將字串中的所有字母轉換為大寫。這個函數不會改變原始的字串,而是返回一個新的轉換後的字串。

string = "Hello, World!"

new_string = string.upper()

print(new_string)  # 輸出: HELLO, WORLD!

lower() 函數

lower() 函數用於將字串中的所有字母轉換為小寫。和 upper() 函數一樣,它也不會改變原始的字串,而是返回一個新的轉換後的字串。

string = "Hello, World!"

new_string = string.lower()


print(new_string)  # 輸出: hello, world!

isupper() 函數

isupper() 函數用於檢查字串中的所有字母是否都是大寫。它返回一個布林值,如果字串中的所有字母都是大寫,則返回 True,否則返回 False。

string = "HELLO"

result = string.isupper()


print(result)  # 輸出: True

pythonCopy codestring = "Hello"

result = string.isupper()


print(result)  # 輸出: False

islower() 函數

islower() 函數用於檢查字串中的所有字母是否都是小寫。它返回一個布林值,如果字串中的所有字母都是小寫,則返回 True,否則返回 False。

string = "hello"

result = string.islower()


print(result)  # 輸出: True

pythonCopy codestring = "Hello"

result = string.islower()


print(result)  # 輸出: False

沒有留言:

張貼留言