簡體   English   中英

在與另一個字符串相同的 position 上插入換行符

[英]Insert line break on same position as in another string

我有兩個字符串:

s1 = '''One scientist
        said'''  

s2 = 'One man said'

我需要像在 s1 中一樣在第二個單詞之后在 s2 中放置 '\n' 符號而不是空格。 麗卡:

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 = '一個人\n說'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM