要將一個字符串中的所有單詞的首字母大寫,可以使用字符串的 title() 方法。這個方法返回一個新的字符串,其中所有單詞的首字母都大寫。
title()
下面是一個例子:
string = "hello world" new_string = string.title() print(new_string)
輸出:
Hello World
在上面的例子中,字符串 "hello world" 中的每個單詞的首字母都被轉換成了大寫。
"hello world"