首页 > 解决方案 > 如何使用 Python 格式化字符串

问题描述

我得到了一个字符串,如下所示

s = 'This is sentence 1."This is sentence 2." This is sentence 3.'

我想要如下的输出

This is sentence 1.
"This is sentence 2."
This is sentence 3.

我为此编写了以下代码

s = 'This is sentence 1."This is sentence 2." This is sentence 3.'
for i in s.replace('.','.\n').split('\n'):
  print(i.strip())

下面是我得到的输出

This is sentence 1.
"This is sentence 2.
" This is sentence 3.

问题在于双引号的句子。

如果我可以编写一个可以区分 . 和。”然后我可以解决我的问题。

标签: pythonpython-3.xregexpython-requests

解决方案


我会尽力给你一个提示。尝试添加一个IF 语句,让你的代码决定你想要做什么。如果您想将代码与更大的字符串一起使用。


推荐阅读