python - How to zero pad an f-string? - Stack Overflow
f-string and string right padding is driving me crazy
Strange behavior: leading zeros in formatted print function are reversed
How can I easily pad a float with x zeros on the left and round/pad to y decimal places on the right?
I know this is dumb, but something is strange. Everything works from the python3 interpreter but in my code it doesn't work. All I want is a string to be padded to the right.
This doesn't work
ename = f'"{endpoint}"'
print(f'{header} {ename:<30}!') I always get a single space before the ! I expect to get this (which I get in the interactive mode)
HEADER ENAME....................!
(note: I put dots there to make the spaces clear, they should be spaces!)
But I get
HEADER ENAME.!
If I change it to this, it works
ename = f'"{endpoint}"'
print(f'{header} {ename:!<30}!')But the padding is ! marks. (which was a test). If I change the ! to a space, that doesn't work.
Clearly I'm being dumb and missing something important. What an I missing? Thanks.
UPDATE: Appears to be caused by some post processing after python runs. Not sure what it is, but I don't believe it's python at the moment. Strange indeed!
UPDATE2: Well, I'm dumb. Problem was that this ultimately went through an HTML renderer and that would remove multiple spaces (exactly as it should). So 100% not a python issue. Thanks to all.
I'm currently learning Python through Harvard's free CS50 course, and while working on the Outdated assignment, I noticed something very bizarre in this specific part below:
while True:
try:
date = input("Date: ")
if date.find("/"):
m, d, y = date.split("/")
print(f"{y}-{m:02}-{d:02}")
except EOFError:
print("")
break
The leading zeros in the identifiers inside the print function result with zeros being in the end, as opposed to the left. For instance, if m is 4, in this case, m will result in 40, instead of 04. In another part of the script using a similar method, this does not occur. Is there something I'm doing wrong? Sorry if its a stupid question, I'm still learning.