文本居中。格式正确吗?

2024-10-03 17:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要这个:

============================= thread: 1 starting ==============================

使用我发现的.format方法实现这一点的唯一方法是:

print("{:=^79}".format(' Thread: ' + self.thread_id + ' starting '))

有没有更好的办法?因为这有点难读,而且有点违背整体的格式原则,左边是字符串,右边是值。你知道吗


Tags: 方法字符串selfidformat格式thread整体
1条回答
网友
1楼 · 发布于 2024-10-03 17:21:55

正如@Felix Lahmer所指出的,您可以使用center

>>> ' Thread: {} starting '.format(42).center(79, '=')
'============================= Thread: 42 starting ============================='

或者可以嵌套format。你知道吗

>>> '{:=^79}'.format(' Thread: {} starting '.format(42))
'============================= Thread: 42 starting ============================='

相关问题 更多 >