5 - 改變字符串首字母的大小寫

字符串首字母大小寫轉換

修改字符串首字母的大小寫

s1 = 'hello'
print(s1)
print(s1.capitalize())

# s1[0] = 'H'  # 只讀的,會拋出異常

# 分片
s1 = s1[0:1] + s1[1].upper() + s1[2:]
print(s1)

s2 = 'Hello'
s = s2[0].lower() + s2[1:]
print(s)
hello
Hello
hEllo
hello

如何將字符中每一個單詞的首字母變成大寫

s3 = 'hello world'
print(s3.capitalize())

# 拆分字符串
arr = s3.split(' ')
# 連接字符串
new_str = f'{arr[0].capitalize()} {arr[1].capitalize()}'
print(new_str)
Hello world
Hello World

6 - 檢測一個字符串是否可以轉換爲數字

發佈了128 篇原創文章 · 獲贊 128 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章