If I have following print statements:
JavaScript
x
6
1
print("#"*80)
2
print(f"##{'':.^76}##")
3
print(f"##{'Hello World':.^76}##")
4
print(f"##{'':.^76}##")
5
print("#"*80)
6
I will get a nice border around my “Hello World” but with dots:
JavaScript
1
6
1
################################################################################
2
##............................................................................##
3
##................................Hello World.................................##
4
##............................................................................##
5
################################################################################
6
If I use a ' '
instead of the .
in the second to fourth print statement, I will get a ValueError: Invalid format specifier
.
How can replace the dot with whitespace or any other ascii symbol?
Advertisement
Answer
Isn’t it enough to simply remove the dot and leave a space?
So:
JavaScript
1
6
1
print("#"*80)
2
print(f"##{'': ^76}##")
3
print(f"##{'Hello World': ^76}##")
4
print(f"##{'': ^76}##")
5
print("#"*80)
6
output will be:
JavaScript
1
6
1
################################################################################
2
## ##
3
## Hello World ##
4
## ##
5
################################################################################
6