Is this what you intended?
>>> print "{:>25.2f}".format(34)
34.00
Note, my spaces may be different to yours (the spaces I have is the same distance from your example).
This is what appears for me:

Is there a better way to use f-strings to add a dollar sign to a right-aligned column of floats where all the decimal places must line up correctly?
Yuck! Mixing f-strings and comma-delimited args to print() is pretty unreadable, plus hard to get right with the extra spaces between arguments. Stick to one f-string per print().
The obvious way to do this (f"${...}") doesn't work the way you want. Run this code:
# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
("gamma", 123.45), ("delta", 1234.56))
for (text, val) in data:
text_str = f"{text}:"
print(f"{text_str:<10}${val:>10.2f}")
# ^^^^^^^^^^^^^ print $ plus value, value right justifiedThis prints:
alpha: $ 1.23 beta: $ 12.34 gamma: $ 123.45 delta: $ 1234.56
Which does right-justify the numeric values, but the $ is in a column of its own.
The way to fix that is use the same trick the code above uses to handle the preceding text on each line. My code has to add the trailing : to each line text. It does that by creating a string with the text and an added final :. Then justify the whole string in the final f-string field. Like this:
# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
("gamma", 123.45), ("delta", 1234.56))
for (text, val) in data:
text_str = f"{text}:"
val_str = f"${val:.2f}"
print(f"{text_str:<10} {val_str:>10s}")
# ^^^^^^^^^^^^^^ print right justified *string*That gives you what you want.
alpha: $1.23 beta: $12.34 gamma: $123.45 delta: $1234.56
Your code won't need that trick for the initial line text, of course.
More on reddit.com
python - align float and specify precision in string formatting - Stack Overflow
How do I right-justify strings with dollar signs and percentages?
Does anyone have a concise cheat sheet for f string formatting with numbers
How do you format a float to two decimal places in Python?
What is the best way to format strings in Python?
Is percent formatting deprecated in Python?
My background: I am in week 3 of an introductory python course for people with no programming experience. I have about a year of self-taught programming knowledge in C & python.
-
Here is the goal outcome: https://i.imgur.com/IvMMrlR.png
The bottom 7 lines (of floats) are all right aligned and have a dollar sign in front of them. My textbook didn't explain how to do it and I don't know enough terminology to be able to google my question. I tried searching for things like how to add multiple format specifiers to an f-string, but I was unable to find anything helpful. Here is how I wound up doing it:
# Converting the floats and ints to strings
checks_written = str(checks_written)
beginning_balance = str("$" + f"{beginning_balance:.2f}")
price_per_check = str("$" + f"{price_per_check:.2f}")
month_fee = str("$" + f"{month_fee:.2f}")
low_fee = str("$" + f"{low_fee:.2f}")
check_fee = str("$" + f"{check_fee:.2f}")
ending_balance = str("$" + f"{ending_balance:.2f}")
total_in_fees = str("$" + f"{total_in_fees:.2f}")
# Printing final 8 lines of finalized fee statements for the user
print(f"{"Checks Written:":<22}", f"{checks_written:>12}", sep="")
print(f"{"Beginning Balance:":<22}", f"{beginning_balance:>12}", sep="")
print(f"{"Check Fee Per Check:":<22}", f"{price_per_check:>12}", sep="")
print(f"{"Month Fee:":<22}", f"{month_fee:>12}", sep="")
print(f"{"Low Fee:":<22}", f"{low_fee:>12}", sep="")
print(f"{"Check Fee:":<22}", f"{check_fee:>12}", sep="")
print(f"{"Ending Balance:":<22}", f"{ending_balance:>12}", sep="")
print(f"{"Total in Fees:":<22}", f"{total_in_fees:>12}", sep="")What would have been the correct way to have done it in a single step rather than converting all my floats to strings first?
-
https://i.imgur.com/aflNR60.png (relevant page from my textbook)
Does python not have a format specification presentation type which says to "put a dollar sign in front of the float"?
Yuck! Mixing f-strings and comma-delimited args to print() is pretty unreadable, plus hard to get right with the extra spaces between arguments. Stick to one f-string per print().
The obvious way to do this (f"${...}") doesn't work the way you want. Run this code:
# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
("gamma", 123.45), ("delta", 1234.56))
for (text, val) in data:
text_str = f"{text}:"
print(f"{text_str:<10}${val:>10.2f}")
# ^^^^^^^^^^^^^ print $ plus value, value right justified
This prints:
alpha: $ 1.23
beta: $ 12.34
gamma: $ 123.45
delta: $ 1234.56
Which does right-justify the numeric values, but the $ is in a column of its own.
The way to fix that is use the same trick the code above uses to handle the preceding text on each line. My code has to add the trailing : to each line text. It does that by creating a string with the text and an added final :. Then justify the whole string in the final f-string field. Like this:
# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
("gamma", 123.45), ("delta", 1234.56))
for (text, val) in data:
text_str = f"{text}:"
val_str = f"${val:.2f}"
print(f"{text_str:<10} {val_str:>10s}")
# ^^^^^^^^^^^^^^ print right justified *string*
That gives you what you want.
alpha: $1.23
beta: $12.34
gamma: $123.45
delta: $1234.56
Your code won't need that trick for the initial line text, of course.
Text is left aligned by default, so you can pad it with {…:22} without the “arrow”
I have an intro Python assignment that asks me to list out a series of numbers with two decimal places and align all the decimal points.
Someone please tell me how I can line these numbers up. Formatting doesn't seem to do literally anything at all.