我简单写了一个,题主看行不行
def convert_initial(old: str) -> str:
new = ""
i = 0
while i < len(old):
if (i == 0) or (old[i - 1] == " "):
new += old[i].upper()
else:
new += old[i]
i += 1
return new
运行示例:
>>> convert_initial("are u ok?")
'Are U Ok?'
>>> convert_initial("who am i?")
'Who Am I?'
>>> convert_initial("here u r.")
'Here U R.'