python基础练习题:Thinkful- 字符串机械:诗格式【难度:1级】--景越Python编程实例训练营,不同难度Python习题,适合自学Python的新手进阶

mac2024-04-07  34

python基础练习题:Thinkful - 字符串机械:诗格式【难度:1级】:

你有可爱的诗集. Unfortuantely他们没有格式化得非常好.他们都在同一行,像这样:

美丽的比丑好.明确优于隐式.简单比复杂好.复杂的是比复杂好.

你需要的是提出一个新行每个句子,这样它看起来像这样:

美丽的比丑好. 明确优于隐式. 简单比复杂好. 复杂的是比复杂好.

写一个函数,format_poem()这需要像一个行示例作为参数字符串,返回一个多行格式化的,每个新的句子上,当你把它打印出来一个新行开始一个新的字符串.

尝试解决与str.split()和[str.join()](HTTPS这一挑战:/ /docs.python.org/3/library/stdtypes.html#str.join)字符串方法.

每一句话都会以句点结束,每一个新句子都会有上期前一个空间.要小心在你的解决方案尾随空白.

编程目标:

def format_poem(poem): # Your code goes here.

测试样例:

Test.assert_equals(format_poem('Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated.'), 'Beautiful is better than ugly.\nExplicit is better than implicit.\nSimple is better than complex.\nComplex is better than complicated.') Test.assert_equals(format_poem("Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules."), "Flat is better than nested.\nSparse is better than dense.\nReadability counts.\nSpecial cases aren't special enough to break the rules.") Test.assert_equals(format_poem("Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess."), "Although practicality beats purity.\nErrors should never pass silently.\nUnless explicitly silenced.\nIn the face of ambiguity, refuse the temptation to guess.") 4

最佳答案(多种解法):

点击查看答案

更多关联题目:

交流讨论:

景越Python基础训练营QQ群 欢迎各位同学加群讨论,一起学习,共同成长!

免责申明:

本博客所有编程题目及答案均收集自互联网,主要用于供网友学习参考,如有侵犯你的权益请联系管理员及时删除,谢谢 题目收集至https://www.codewars.com/ https://www.codewars.com/kata/thinkful-string-drills-poem-formatter

最新回复(0)