首页 > 解决方案 > Python,在解析字符串时,如果字符则换行

问题描述

如果我有一个如下所示的字符串(出于示例目的而简化)

myString = 

"Hello world, the weather is nice today} How are you today?"

有没有一种方法可以解析字符串,并且

if "}" then \n

所以我的最终结果是,

"Hello world, the weather is nice today}
How are you today?"

标签: python

解决方案


是的,使用replace()方法:

myString = "Hello world, the weather is nice today} How are you today"

print(myString.replace('}', '}\n'))

输出:

Hello world, the weather is nice today}
 How are you today

推荐阅读