python - 两个多行字符串的水平连接
问题描述
我目前尝试水平连接两个多行字符串。例如,有两个字符串 str_a 和 str_b
str_a = """This is row \nAnd this is row\nThis is the final row"""
str_b = """A\nB"""
随着打印返回
This is row
And this is row
This is the final row
和
A
B
水平连接后结果字符串的打印返回应如下所示
This is row A
And this is row B
This is the final row
解决方案
用这个:
import itertools
for a, b in itertools.zip_longest(str_a.split('\n'), str_b.split('\n')):
print(a, b if b else '')
输出:
This is row A
And this is row B
This is the final row
推荐阅读
- c# - MaterialDesignInXaml如何实现滚动控制
- r - 如何使 ggmap 填满 R Shiny 应用程序的整个页面?
- javascript - 获取对象数组中的特定元素
- active-directory - AD 单值自定义属性在 powershell 中返回为多值
- c# - 数组的 GCHandle.AddrOfPinnedObject 是否总是返回指向数组第一个元素的指针?
- r - 将顶点标签放置在圆形图的外部,用于不同大小的顶点和不同大小的标签
- java - 从java中给定的一组数字中查找最大日期和时间
- java - 如何通过透明窗口从屏幕读取数据
- swift - UserDefault.standard 首选项仅正确保存约 80% 的时间
- mysql - 根据 col2 的分组值捕获 col1 的值