Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting string format

I'm writing some reports, and I'd like to know if there's a simpler way to obtain the following behavior.

>>> '{:-^60}'.format('Percentage used: {:.2%}'.format(.4))
'------------------Percentage used: 40.00%-------------------'

As you can see I'm centering the text and then printing a number formated to percentage. But I'm using a format function inside another. If possible I'd like to do the same in a single function, kind of the following.

'$SOMETHING GOES HERE'.format(header = 'Percentage Used:',percentage = .4)

Of course I'm looking for a general solution, that would work with all, or most, of the formating options, not just to alignment

Thanks.

like image 901
Darkade Avatar asked Sep 28 '12 03:09

Darkade


1 Answers

A more readable option might be str.center

>>> 'Percentage used: {:.2%}'.format(.4).center(60, '-')
'------------------Percentage used: 40.00%-------------------'
like image 154
wim Avatar answered Oct 16 '22 06:10

wim