在行之间添加破折号

时间:2015-12-08 16:26:22

标签: python

所以我有一个我想打印出来的代码:

----------
Tom Cruise
Los Angeles, CA
http://www.tomcruise.com
Official TomCruise.com crew tweets. We love you guys! 
Visit us at Facebook!
['katieH', 'NicoleKidman']
----------
Perez Hilton
Hollywood, California
http://www.PerezH...
Perez Hilton is the creator and writer of one of the most famous websites
in the world. And he also loves music - a lot!
['tomCruise', 'katieH', 'NicoleKidman']
----------

但是,我不知道如何添加破折号以便它看起来像那样。如果我做了像

这样的事情
dashes = '----------\n'
result = "\n".join((str(v) for v in lst))
combined = dashes + result
print(result)

它看起来像:

----------
Tom Cruise
Los Angeles, CA
http://www.tomcruise.com
Official TomCruise.com crew tweets. We love you guys! 
Visit us at Facebook!
['katieH', 'NicoleKidman']
----------
Perez Hilton
Hollywood, California
http://www.PerezH...
Perez Hilton is the creator and writer of one of the most famous   websites
in the world. And he also loves music - a lot!
['tomCruise', 'katieH', 'NicoleKidman']

但是,我需要在最后添加一行破折号,如果我打印(结果+短划线),它就会变得很时髦。我该怎么办?

1 个答案:

答案 0 :(得分:1)

你真的在第二个\n之前错过dashes吗?

print(dashes + result + '\n' + dashes)

虽然我必须说这似乎是一种奇怪的做事方式;为什么不单独打印每件东西?根本不需要换行符。

dashes = '----------'
print(dashes)
for line in lst:
    print(line)
print(dashes)