下面小編就為大家帶來一篇Python 字串大小寫轉換的簡單實例。小編覺得蠻不錯的,現在就分享給大家,也給大家做個參考。一起跟著小編過來看看吧
①所有字母都轉換為大寫
# -*- coding:utf-8 -* -
if __name__ == "__main__":
a = 'hello, world!'
print(a.upper())輸出:
HELLO, WORLD!
②所有字母都轉換為小寫
# -*- coding:utf-8 -*-
if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.lower())輸出:
hello, world!
③首字母轉換成大寫, 其餘轉換成小寫
# -*- coding:utf-8 -*-
if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.capitalize())輸出:
Hello, world!
④所有單字的首字母轉換成大寫, 其餘轉換成小寫
# -*- coding:utf-8 -*-
if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.title())輸出:
Hello, World!
⑤判斷所有字母都為大寫
# -*- coding:utf-8 -*-
if __name__ = = "__main__":
a = 'HELLO, WORLD!'
print(a.isupper())
b = 'hello, world!'
up
up ))輸出:
False
⑥判斷所有字母都為小寫
# -*- coding:utf-8 -*-
if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.islower())
print(b.islower())輸出:
True
⑦判斷所有單字的首字母為大寫
# -*- coding:utf-8 -*-
if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.istitle())
b = 'hello, world!'
print(b.b.p. -+ c = 'Hello, World!'
print(c.istitle())輸出:
False
False
True
以上這篇Python 字串大小寫轉換的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援PHP中文網。
更多Python 字串大小寫轉換的簡單實例相關文章請關注PHP中文網!