在与另一个字符串相同的位置插入换行符



我有两个字符串:

s1 = '''One scientist
said'''  
s2 = 'One man said'

我需要在s2中的第二个单词后面加上"\n"号,而不是s1中的空格。喜欢:

s2 = '''One man 
said'''

但是我该怎么做呢?

试试这个:

s2 = 'One man said'
x = s2.split()[:2] # First two words
y = s2.split()[2:] # After first two
text_to_add = 'n        ' # Change if you want
out = x + text_to_add + y
print(out)

试试这个解决方案,它真的很简单:

s2 = 'One mann said'

最新更新