Granitosaurus
Granitosaurus

Reputation: 21436

centering text using .format the right way?

I want this:

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

the only way to achieve this with the .format method that I found is:

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

is there a better way to do this? since this is a bit hard to read and kinda goes against the whole .format principle of having string on the left and values on the right.

Upvotes: 1

Views: 63

Answers (1)

timgeb
timgeb

Reputation: 78690

As @Felix Lahmer has pointed out, you can use center:

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

Or you could nest format.

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

Upvotes: 2

Related Questions