I am trying to centre the following ( didn't want to include the whole code as its 166 lines long)
for years in range (1,5):Payments = years * 12FinFee = (39.99) * yearsTotPrice = TotalSalesPrice + FinFeeMonPay = TotPrice / 12
FinFeeDSP = "${:,.2f}".format(FinFee)TotPriceDSP = "${:,.2f}".format(TotPrice)MonPayDSP = "${:,.2f}".format(MonPay)print(f" {years:^2d} {Payments:^9d} {FinFeeDSP:^9s} {TotalSalesPriceDSP:^9s} {MonPayDSP:^9s}")print(f"------------------------------------------------------------------------")CurDate = datetime.datetime.now()CurDatePlus30 = CurDate + datetime.timedelta(days = 30)print(f"Invoice date: {CurDate.strftime('%d-%b-%y').upper()} First payment date: {CurDatePlus30.strftime('%d-%b-%y').upper()}")print()print()
Maybe centre isn't the right word, but I'm trying to get the f strings with equal padding/space on each side. Right now it lines up with the printed f-strings above it and I need it more in the middle.
It currently looks like this: https://imgur.com/a/hpBBcLN
I need the table on the bottom entered or indented so there is equal space on both sides
python - How would I define a fill character for f-string alignment? - Stack Overflow
f-string alignment
How to align text centered on a character?
Help centering multiple f strings?
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.
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.
Hi guys,
Can anyone help me on how I can align text in a string so it is centered on the ": "?
I was thinking something like string concatenation but not sure how to align it so I would not be blocked by a character limit on the left side.
I need to create a function that outputs the following:
Toyota Prius Current: 12500km Last service: 11000km Next service: 21000km Service due: False Toyota Prius Current: 12500km Last service: 11000km Next service: 21000km Service due: False