vec*_*ife 2 python letter capitalize
我有一个字符串,必须在“!”后使用大写字母:
我编写了一个可以在一定程度上起作用的脚本,但是当最后一个字母为“!”时出现了问题。
strin "hello! there!"
strout = []
for i in range(len(strin)):
if strin[i-2] == '!':
strout.append((strin[i]).capitalize())
else:
strout.append(strin[i])
strout[0] = strout[0].capitalize()
newStr = "".join(strout)
Run Code Online (Sandbox Code Playgroud)
输出是:你好!那里!
如何防止第二个字母大写。
原因[i-2]是每当循环遇到“!” 在文本中间,它大写字母i。
那这个呢:
string = "hello! there!"
'! '.join(map(lambda s: s.lstrip().capitalize(), string.split('!')))
Run Code Online (Sandbox Code Playgroud)