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:

Answer from TerryA on Stack Overflow
🌐
Real Python
realpython.com › how-to-python-f-string-format-float
How to Format Floats Within F-Strings in Python – Real Python
March 18, 2026 - As you can see, your float has been rounded to two decimal places. You achieved this by adding the format specifier .2f into the replacement field. The 2 is the precision, while the lowercase f is an example of a presentation type. You’ll see more of these later. Note: When you use a format specifier, you don’t actually change the underlying number. You only improve its display. Python’s f-strings also have their own mini-language that allows you to format your output in a variety of different ways.
🌐
Built In
builtin.com › data-science › python-f-string
Guide to String Formatting in Python Using F-strings | Built In
Syntax: {:[width][.precision][type]} Type: d: integers f: floating point numbers *You need to specify precision only in case of floating point numbers · More on Python: 5 Types of Arguments in Python Function Definitions · There are very few occasions when you’d need to align a word or text to the right or left, but this is the foundation to fully understanding how to add zeros to the left or right of a number. F-string is very useful when formatting numbers.
Discussions

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 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.

More on reddit.com
🌐 r/learnpython
8
5
September 12, 2024
python - align float and specify precision in string formatting - Stack Overflow
Works great with f-strings too. More on stackoverflow.com
🌐 stackoverflow.com
How do I right-justify strings with dollar signs and percentages?
"Formatting doesn't seem to do literally anything at all." Well, all of Python's number formatting methods allow you to restrict the output to two decimal places and to right justify it. I suggest that you post your code so that people here can advise you what is going wrong with it. You can edit your original post to include it. Guide to posting code in this subreddit: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F More on reddit.com
🌐 r/learnpython
8
3
February 13, 2022
Does anyone have a concise cheat sheet for f string formatting with numbers
Here's what I have as a reference. Basically a summary of the Format Specification Mini-Language . This would all go after a : in the f-string curly braces. So if you want a float with 2 decimal place precision, with * as fill and right aligned in a field width of 10 characters you'd do f"{val:*>10.2f}" [[fill]align][sign][#][0][minimumwidth][.precision][type] Fill: Add a character to fill with. Must be followed by Align flag. Align: '<' - Forces the field to be left-aligned within the available space (This is the default.) '>' - Forces the field to be right-aligned within the available space. '=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form '+000000120'. This alignment option is only valid for numeric types. '^' - Forces the field to be centered within the available space. Sign: '+' - indicates that a sign should be used for both positive as well as negative numbers '-' - indicates that a sign should be used only for negative numbers (this is the default behavior) ' ' - indicates that a leading space should be used on positive numbers #: Flags alternate numbering formats; binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively. 0: Zero-padding. Equivalent to fill '=' and character of '0' Minimumwidth: Min width of the field. Precision: Decimal places for floats or max field size for non-numeric types. Type: Integers: 'b' - Binary. Outputs the number in base 2. 'c' - Character. Converts the integer to the corresponding Unicode character before printing. 'd' - Decimal Integer. Outputs the number in base 10. 'o' - Octal format. Outputs the number in base 8. 'x' - Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' - Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' - Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. '' (None) - the same as 'd' Floats: 'e' - Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent. 'E' - Exponent notation. Same as 'e' except it converts the number to uppercase. 'f' - Fixed point. Displays the number as a fixed-point number. 'F' - Fixed point. Same as 'f' except it converts the number to uppercase. 'g' - General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. 'G' - General format. Same as 'g' except switches to 'E' if the number gets to large. 'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' - Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. '' (None) - similar to 'g', except that it prints at least one digit after the decimal point. More on reddit.com
🌐 r/learnpython
5
3
February 2, 2022
People also ask

How do you format a float to two decimal places in Python?
Use f"{value:.2f}" in modern code, "{:.2f}".format(value) with str.format(), or "%.2f" % value in percent-style formatting.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
What is the best way to format strings in Python?
Use f-strings in Python 3.6 and later for most new code. They are readable and support variables, expressions, and format specifiers inside braces.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
Is percent formatting deprecated in Python?
No. It is old-style or legacy formatting, but Python still documents printf-style string formatting for reading and maintaining older code.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
🌐
Bentley
cissandbox.bentley.edu › sandbox › wp-content › uploads › 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
There are several options when formatting floating point numbers. The number of decimal · places, the use of the number separator character, and forcing a plus (+) or minus (-) sign can · also be specified. These are shown in the following example: ... Program output is often required to be in tabular form. f-strings are very useful in formatting · this kind of output. The following lines produce a tidily aligned ...
🌐
Reddit
reddit.com › r/learnpython › 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?
r/learnpython on Reddit: 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?
September 12, 2024 -

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"?

Top answer
1 of 3
15

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.

2 of 3
1

Text is left aligned by default, so you can pad it with {…:22} without the “arrow”

🌐
Towards Data Science
towardsdatascience.com › home › programming › formatting strings and numbers in python
Formatting strings and numbers in python | Towards Data Science
June 27, 2021 - It overcomes the limitation that ... the 'order-problem' You can use the :>, :< or :^ option in the f-format to left align, right align or center align the text that you want to format....
Find elsewhere
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python string format
Python String Format: f-strings, format(), %, Padding, and Precision
June 23, 2026 - Each line builds a formatted string with a different style. For appending text without full formatting rules, see Append string in Python. String formatting inserts values into a string. It can also control width, alignment, padding, decimal places, signs, commas, percentages, and dates.
🌐
GeeksforGeeks
geeksforgeeks.org › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
March 22, 2025 - Python’s f-strings allow text alignment using three primary alignment specifiers:
🌐
ZetCode
zetcode.com › python › fstring
Python f-string - formatting strings in Python with f-string
May 11, 2025 - $ python main.py Always show sign: +42 -42 +0 Default behavior: 42 -42 0 Space for positive: 42 -42 0 Aligned column: +42 -42 +0 · F-strings can also format complex numbers, allowing you to control the display of their real and imaginary parts. You can specify precision for both parts independently if needed, though standard float formatting options apply.
🌐
Medium
thetechplatform.medium.com › fstring-in-python-the-complete-guide-to-fstring-formatting-900676a768d5
Fstring in Python: The Complete Guide to Fstring Formatting | by The Tech Platform | Medium
February 12, 2024 - The curly braces {} contain the variable names, and Python replaces these placeholders with the actual values of the variables. The output is: “My name is Alice and I am 30 years old.” · Formatting Options: Fstring also supports formatting options, which allow you to control the appearance of variables within the string. For example, you can specify the number of decimal places for floating-point numbers or control the width of string representations.
🌐
OneUptime
oneuptime.com › home › blog › how to use f-strings for string formatting in python
How to Use f-strings for String Formatting in Python
January 25, 2026 - F-strings provide powerful number formatting options: # Integer formatting num = 42 print(f"Binary: {num:b}") # Binary: 101010 print(f"Octal: {num:o}") # Octal: 52 print(f"Hex: {num:x}") # Hex: 2a print(f"Hex upper: {num:X}") # Hex upper: 2A # Padding with zeros print(f"Padded: {num:05d}") # Padded: 00042 # Float formatting pi = 3.14159265359 print(f"Default: {pi}") # Default: 3.14159265359 print(f"2 decimals: {pi:.2f}") # 2 decimals: 3.14 print(f"5 decimals: {pi:.5f}") # 5 decimals: 3.14159 # Scientific notation large = 1234567890 print(f"Scientific: {large:e}") # Scientific: 1.234568e+09 print(f"Scientific: {large:.2e}") # Scientific: 1.23e+09 # Percentage ratio = 0.756 print(f"Percentage: {ratio:.1%}") # Percentage: 75.6%
🌐
GitHub
github.com › AHunter2026 › python-materials › blob › master › formatting-floats-f-strings › README.md
python-materials/formatting-floats-f-strings/README.md at master · AHunter2026/python-materials
These materials are for use with ... Format Floats Within F-Strings in Python · The code plus exercise solutions are all presented inside the Code_and_Solutions.ipynb Jupyter Notebook. To view the file, you will need to install either Jupyter Notebook or JupyterLab. Note: Some minor differences exist between the code presented in the tutorial and those in the Jupyter Notebook. This is necessary to align the output ...
Author: AHunter2026
🌐
Miguendes
miguendes.me › 73-examples-to-help-you-master-pythons-f-strings
Python F-String: 73 Examples to Help You Master It
July 2, 2022 - Learn Python f-strings by example. Use string interpolation to format float precision, multiline strings, decimal places, hex and other objects.
🌐
Real Python
realpython.com › python-f-strings
Python's F-String for String Interpolation and Formatting – Real Python
November 30, 2024 - The f letter tells the operator to convert to a floating-point number. The .2 part defines the precision to use when converting the input. In the second example, you use %5s to align the age value five positions to the right.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Use the rjust(), center(), or ljust() methods to right-justify, center, or left-justify strings str in Python. You can convert numbers (int and float) to strings using str() and apply these methods. R ...
🌐
AskPython
askpython.com › python › string › python-floating-point-formatting
Python Floating Point Formatting: 2 Simple Methods - AskPython
January 18, 2024 - We can face difficulties like unexpected rounding and alignment errors. Thus we should test formatting on various inputs to ensure it behaves as expected in different scenarios. We can use the ‘format’ method to specify the width and assignment of the floating numbers. You can limit a float to 2 decimal places using either f-strings or the format() method.
🌐
Python Morsels
pythonmorsels.com › string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - All the above options were about the format specifications (the part after a : within a format string replacement field). Format specifications are object-specific (str, int, float, datetime all support a different syntax).
Top answer
1 of 2
3
Here's what I have as a reference. Basically a summary of the Format Specification Mini-Language . This would all go after a : in the f-string curly braces. So if you want a float with 2 decimal place precision, with * as fill and right aligned in a field width of 10 characters you'd do f"{val:*>10.2f}" [[fill]align][sign][#][0][minimumwidth][.precision][type] Fill: Add a character to fill with. Must be followed by Align flag. Align: '<' - Forces the field to be left-aligned within the available space (This is the default.) '>' - Forces the field to be right-aligned within the available space. '=' - Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form '+000000120'. This alignment option is only valid for numeric types. '^' - Forces the field to be centered within the available space. Sign: '+' - indicates that a sign should be used for both positive as well as negative numbers '-' - indicates that a sign should be used only for negative numbers (this is the default behavior) ' ' - indicates that a leading space should be used on positive numbers #: Flags alternate numbering formats; binary, octal, and hexadecimal output will be prefixed with '0b', '0o', and '0x', respectively. 0: Zero-padding. Equivalent to fill '=' and character of '0' Minimumwidth: Min width of the field. Precision: Decimal places for floats or max field size for non-numeric types. Type: Integers: 'b' - Binary. Outputs the number in base 2. 'c' - Character. Converts the integer to the corresponding Unicode character before printing. 'd' - Decimal Integer. Outputs the number in base 10. 'o' - Octal format. Outputs the number in base 8. 'x' - Hex format. Outputs the number in base 16, using lower- case letters for the digits above 9. 'X' - Hex format. Outputs the number in base 16, using upper- case letters for the digits above 9. 'n' - Number. This is the same as 'd', except that it uses the current locale setting to insert the appropriate number separator characters. '' (None) - the same as 'd' Floats: 'e' - Exponent notation. Prints the number in scientific notation using the letter 'e' to indicate the exponent. 'E' - Exponent notation. Same as 'e' except it converts the number to uppercase. 'f' - Fixed point. Displays the number as a fixed-point number. 'F' - Fixed point. Same as 'f' except it converts the number to uppercase. 'g' - General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. 'G' - General format. Same as 'g' except switches to 'E' if the number gets to large. 'n' - Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters. '%' - Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign. '' (None) - similar to 'g', except that it prints at least one digit after the decimal point.
2 of 2
1
A cheat sheet for f-string would be too short and kind of useless since f-string are super simple. If you want to put a number just do it. Ex. name: str = "Albus Dumbledore" age: int = 116 print(f"Name: {name}, Age: {age}") As you can see, I simply put age (which is an int) inside 2 brackets in the string (don't forget the f) and let Python do it's magic. (If you want you can use the example above as your cheat sheet)