Python-在新行上打印每个句子 [英] Python - Print Each Sentence On New Line
本文介绍了Python-在新行上打印每个句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
针对每个主题,我尝试将字符串中的每个句子打印在新行上.使用下面显示的当前代码和输出,返回下面显示的正确输出"的语法是什么?
Per the subject, I'm trying to print each sentence in a string on a new line. With the current code and output shown below, what's the syntax to return "Correct Output" shown below?
代码
sentence = 'I am sorry Dave. I cannot let you do that.'
def format_sentence(sentence):
sentenceSplit = sentence.split(".")
for s in sentenceSplit:
print s + "."
输出
I am sorry Dave.
I cannot let you do that.
.
None
正确的输出
I am sorry Dave.
I cannot let you do that.
推荐答案
您可以执行以下操作:
def format_sentence(sentence) :
sentenceSplit = filter(None, sentence.split("."))
for s in sentenceSplit :
print s.strip() + "."
这篇关于Python-在新行上打印每个句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文