Specify it before the alignment character (> or <), without apostrophes.
contents = {"Introduction": 1, "Python Basics": 5, "Creating Your First Program": 12, "Operators and Variables": 29}
for chapt, page in contents.items():
print(f"{chapt:.<30}{page:.>5}")
Outputs:
Introduction......................1
Python Basics.....................5
Creating Your First Program......12
Operators and Variables..........29
From the documentation
Answer from pho on Stack OverflowIf a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (β
{β or β}β) as the fill character in a formatted string literal or when using thestr.format()method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesnβt affect theformat()function
Specify it before the alignment character (> or <), without apostrophes.
contents = {"Introduction": 1, "Python Basics": 5, "Creating Your First Program": 12, "Operators and Variables": 29}
for chapt, page in contents.items():
print(f"{chapt:.<30}{page:.>5}")
Outputs:
Introduction......................1
Python Basics.....................5
Creating Your First Program......12
Operators and Variables..........29
From the documentation
If a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (β
{β or β}β) as the fill character in a formatted string literal or when using thestr.format()method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesnβt affect theformat()function
According to the Format Specification Mini-Language you have to specify it before the "align":
contents = [('Introduction', 1), ('Python Basics', 5),
('Creating Your First Program', 12),
('Operators and Variables', 29)]
row_length = 35
for chapt, page in contents:
dots = str(row_length - len(str(page)))
print(f"{chapt:.<{dots}}{page}")
*This is now independent of the page number's length. A little bit more flexible.
f-strings in Python 3.6 are awesome
f-string alignment
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
How to align text centered on a character?
I try to align multiple line of strings with print(f"{string:25}"), their lengths are the same, but they don't look aligned in Jupyter noteook, see screenshot: https://imgur.com/a/Oa1isra
Please kindly advise why this is the case.
When f-strings were announced I was dubious - .format() is pretty neat, why bother with anything else?
Turns out they are awesome. There's not really much to say beyond that - they don't magically do things that .format() doesn't. But once you start using them, you get hooked. It just feels so surprisingly natural, you wonder why you ever did it any other way. If you don't believe me, just try it!
Kudos too to PyCharm for the syntax highlighting and autocompletion support - hope every editor picks up support soon (Jupyter notebooks, I'm looking at you!).