def 首字母转大写(字符串):
单词列表 = []
for 单词 in 字符串.split(" "):
单词列表.append(单词[0].upper()+单词[1:])
return " ".join(单词列表)
字符串 = "we will be OK!"
新字符串 = 首字母转大写(字符串)
print(新字符串)
1、先通过空格分隔单词
2、然后将分出单词每个首字母变为大写
3、最后将分出的单词通过空格拼接
'hello world !'.title()
'we will be OK!'.title()
输出:
'Hello World !'
'We Will Be Ok!'
def initialsToupper(str): a = [] for t in str.split(" "): a.append(t[0].upper()+t[1:]) return " ".join(a) # str = "we will be OK!" str = input("请输入一串字符串,单词之间用空格分隔:"); newStr = initialsToupper(str) print(newStr)
txt = 'hello world !'.split(' ')
print(' '.join([i[:1].upper() + i[1:] for i in txt]))
txt = 'we will be OK!'.split(' ')
print(' '.join([i[:1].upper() + i[1:] for i in txt]))
好了