Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

placing f-strings inside of f-strings

I have a number 1234567.12. I’m trying to center it in a field of 20, with 3 decimal places, commas as separators and pad the left and right sides with *. Here’s what I did: (Python3.11)

value = 1234567.12
y = f'{value:,.3f}'
x = f'{y:*^20}'

which is also equivalent to:

x = f'{f"{value:,.3f}":*^20}'

is there a cleaner syntax?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

According to the specifications of the format mini-language:

format_spec     ::=  [[fill]align][sign]["z"]["#"]["0"][width][grouping_option]["."precision][type]

you can just put fill (*), align (^) and width (20) before grouping_option (,):

x = f'{value:*^20,.3f}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading