30秒学会 Python 片段 · 2023年7月10日

30秒学会 Python 片段 – Capitalize every word

Capitalizes the first letter of every word in a string.

  • Use str.title() to capitalize the first letter of every word in the string.

代码实现

def capitalize_every_word(s):
  return s.title()

使用样例

capitalize_every_word('hello world!') # 'Hello World!'

翻译自:https://www.30secondsofcode.org/python/s/capitalize-every-word