更新时间:2022-01-13 06:58:28
一种简单的方法是用新行和11替换新行(由于 {:< 10}中的10,所以将其替换为11)
,但您在格式中添加了一个额外的空格)空格:
An easy way to achieve this is replacing a new line with a new line and 11 (11 because of the 10 in {:<10}
but you add an additional space in your format) spaces:
B2 = B.replace('\n','\n ')
print('{:<10} {}'.format(A, B2))
或更优雅:
B2 = B.replace('\n','\n'+11*' ')
print('{:<10} {}'.format(A, B2))
在 python3
中运行它:
$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> A = '[A]: '
>>> B = 'this is\na string\nwith a new line'
>>> B2 = B.replace('\n','\n ')
>>> print('{:<10} {}'.format(A, B2))
[A]: this is
a string
with a new line