Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想打印一个超过 1 行的字符串居中
a = "*\n*\n**"
我试过了
print '{0:^20}'.format(a, 'centered')
但它只是把第一个*放在中心,我怎样才能把所有的字符串放在中心?
它实际上确实使整个字符串居中:
>>> '{0:^20}'.format(a, 'centered') ' *\n*\n** '
请注意,这会在 之前和之后放置七个空格a。我认为您希望它以每行的内容为中心,您可以这样做:
a
>>> print '\n'.join('{0:^20}'.format(x, 'centered') for x in a.split('\n')) * * **