I want to align a combination of str and float to the right side. In my example, "Tax = $" is a string and tax is a float. I have two ways to accomplish this.
print(("Tax = $%.2f" % tax).rjust(70))
print("%66s%.2f" % ("Tax = $",tax))
The output is the following:
Tax = $0.42
Both of them are working. But I don't think they are good enough. The codes are kind of burdensome. At the beginning, I wrote something like
print("Tax = $%.2f" % tax)
I tried to put a right align flag in this line, but I don't know where should I put it in.
Is there a neat way to do this?
Thank you,
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.