Now with f-strings available in python 3.6 and newer, here is another way to look at it:

print(f"{(f'Tax = ${tax:.2f}'):>70}")

Using temporary variables:

rightSideStuff = f'Tax = ${tax:.2f}'
print(f"{rightSideStuff:>70}")

The resource I used for finding how to align right: https://medium.com/@NirantK/best-of-python3-6-f-strings-41f9154983e

Answer from KemptCode on Stack Overflow
🌐
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 ways to align variables in f-strings. The various alignment options are as ... Forces the expression within the curly braces to be left-aligned. This is the ... Forces the expression within the curly braces to be right-aligned.
Discussions

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
Python: Format string with float, right alignment - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I want to align a combination of str and float to the right side. In my example, "Tax = $" is a string ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Python’s f-strings allow text alignment using three primary alignment specifiers: < (Left Alignment): Aligns the text to the left within the specified width. > (Right Alignment): Aligns the text to the right within the specified width.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - print(f'right : {i:08}') # right : 00000255 print(f'right : {i:08x}') # right : 000000ff · source: rjust_center_ljust.py · Python · String · Convert a String to a Number (int, float) in Python · Raw Strings in Python · Regular Expressions in Python: the re Module ·
Find elsewhere
🌐
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 - 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. ... After formatting the alignment of output text ...
🌐
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.
🌐
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”

🌐
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 - Here are the common symbols for string alignment and padding in Fstring: <: Left-align the string within the specified width. ^: Center-align the string within the specified width. >: Right-align the string within the specified width.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-format-a-floating-number-to-fixed-width-in-python
How to format a floating number to fixed width in Python?
March 24, 2026 - number = 838.65 # Right align with ... Padding:', zero_padding) ... Use f-strings for modern, readable formatting with f"{number:10.2f}" syntax....
🌐
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 - Strings don’t understand precision. You’ll learn more about using strings with format specifiers later in this tutorial. You can also control the placement of the padding if you need to. It’s possible to left, right, and even center-align your data within the specified width:
🌐
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.
🌐
Stack Abuse
stackabuse.com › formatting-strings-with-python
Formatting Strings with Python
July 17, 2023 - Python 3.6: >>> f"{left_aligned:><15}{center:-^10}{right_aligned:<>15}" 'Left Align>>>>>-Centered-<<<<Right Align' Formatting floating point numbers, those containing decimal places, is a cinch in Python. All you need to do is follow the colon with an f. Python > 2.6: >>> rounded_pi = 3.14 >>> "A rounded representation of Pi {:f}".format(rounded_pi) 'A rounded representation of Pi 3.140000' Free eBook: Git Essentials ·
🌐
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 - text = "Python" # Minimum width with alignment print(f"|{text:10}|") # |Python | (left, default for strings) print(f"|{text:<10}|") # |Python | (left) print(f"|{text:>10}|") # | Python| (right) print(f"|{text:^10}|") # | Python | (center) # Custom fill character print(f"|{text:*<10}|") # |Python****| print(f"|{text:*>10}|") # |****Python| print(f"|{text:*^10}|") # |**Python**| # Numbers are right-aligned by default num = 42 print(f"|{num:10}|") # | 42| print(f"|{num:<10}|") # |42 |
🌐
YouTube
youtube.com › watch
Python Format string with float right alignment
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new features
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)
🌐
DEV Community
dev.to › bybydev › advanced-string-formatting-with-f-strings-in-python-3a8b
Advanced string formatting with f-strings in Python - DEV Community
May 7, 2024 - price = 123.456789 # Format to ... ${price:.0f}" print(formatted_price_int) # Output: Price (rounded): $123 · You can use :<w, :>w, or :^w to align a string to the left, right, or center within a given width w, where w is an ...
🌐
Saralgyaan
saralgyaan.com › posts › f-string-in-python-usage-guide
Python f-strings - The Ultimate Usage Guide
August 20, 2019 - ladbrokes立博-公司官网 版权所有 地址:ladbrokes立博 邮编:250100 电话:0534-8985830