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

Is it possible to use whitespace in format specifier

If I have following print statements:

print("#"*80)
print(f"##{'':.^76}##")
print(f"##{'Hello World':.^76}##")
print(f"##{'':.^76}##")
print("#"*80)

I will get a nice border around my "Hello World" but with dots:

################################################################################
##............................................................................##
##................................Hello World.................................##
##............................................................................##
################################################################################

If I use a ' ' instead of the . in the second to fourth print statement, I will get a ValueError: Invalid format specifier.

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

How can replace the dot with whitespace or any other ascii symbol?

>Solution :

Isn’t it enough to simply remove the dot and leave a space?

So:

print("#"*80)
print(f"##{'': ^76}##")
print(f"##{'Hello World': ^76}##")
print(f"##{'': ^76}##")
print("#"*80)

output will be:

################################################################################
##                                                                            ##
##                                Hello World                                 ##
##                                                                            ##
################################################################################
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