簡體   English   中英

在 python 中合並兩個多行字符串

[英]Combine two multi line strings in python

我目前有兩個字符串,可以說它們是

1
2
3
4
5

一個
b
c
d
e

我希望將字符串組合成

1a
2b
3c
4d
5e

我將如何 go 在 python 中執行此操作?

您可以嘗試使用zipsplitjoin進行一些理解:

'\n'.join(f'{x}{y}' for x,y in zip(string1.split('\n'), string2.split('\n')))

OUTPUT

1a
2b
3c
4d
5e

您可以使用以下代碼執行此操作:

str1 = "1\n2\n3\4\n5" 
str2 = "a\nb\nc\nd\ne"
# \n means new line in python 
lst1 = str1.split("\n")
lst2 = str2.split("\n")
result_lst = [] 
for i, j in zip(lst1, lst2):
   result_lst.append(i + j)
new_line = "\n" 
result_str = new_line.join(result_lst)
print(result_str)

這將為您提供以下 output:

1a
2b
3c
4d
5e

暫無
暫無

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

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