出一道python编码题,并给出答案
题目:编写一个函数,输入一个字符串,输出该字符串中每个单词的首字母大写的字符串。
示例:
输入:"hello world"
输出:"Hello World"
答案:
def capitalize_words(string):
words = string.split()
capitalized_words = [word.capitalize() for word in words]
return " ".join(capitalized_words)
print(capitalize_words("hello world"))