如果我没有提供足够的信息,请让我知道。该程序的目标是将每个句子的第一个字母大写。
usr_str = input()
def fix_capitalization(usr_str):
list_of_sentences = usr_str.split(".")
list_of_sentences.pop() #remove last element: ""
new_str = ''
for sentence in list_of_sentences:
new_str += sentence.capitalize() + "."
return new_str
print(fix_capitalization(usr_str))例如,如果我输入“嗨。你好。嘿”。我希望它会输出"Hi。Hello。嘿“。但是,它会输出"Hi。hello。嘿“。
发布于 2020-10-17 02:48:22
另一种方法是构建一个字符串列表,然后将它们连接起来:
def fix_capitalization(usr_str):
list_of_sentences = usr_str.split(".")
output = []
for sentence in list_of_sentences:
new_sentence = sentence.strip().capitalize()
# If empty, don't bother
if new_sentence:
output.append(new_sentence)
# Finally, join everything
return ". ".join(output) +"."发布于 2020-10-17 02:49:16
您输入的句子之间有空格。现在,当您拆分列表时,列表位于“.”字符空格仍然保留。我检查了拆分列表时列表中的元素是什么,结果是这样的。
“”“
‘嗨’,‘你好’,‘嘿’,'‘
“”“
https://stackoverflow.com/questions/64394731
复制相似问题