Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegant way to format single string having multiple call to `format()` (format chaining)

I am trying to substitute a variable using format() and then format the resulting string using format().

This is what I ended up doing:

>>> '{:^50}'.format("Missing files for device : {0}".format(var))
'          Missing files for device : abc          '

where var is a variable holding 'abc'. Is there a better way to get the same result?

like image 903
Ankur Agarwal Avatar asked Dec 05 '25 05:12

Ankur Agarwal


1 Answers

In your case, there is a simpler way:

>>> "Missing files for device : {0}".format(var).center(50)
'          Missing files for device : abc          '

Calling format twice is not necessary here.

like image 200
wim Avatar answered Dec 09 '25 14:12

wim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!