I've formatted following string
Formatted = "My profit is {:.0%}."
print(Formatted.format(0.25))
The output is: My profit is 25%.
How can I change the output into: My profit is 25 %
.
I've allready added (between 25 and %): "," "space" ":" "() and other things but it always give an error.
Thanks beforhand for your help.
The %
formatter does not have any options to add spacing.
You can use the f
formatter instead, multiply the value by 100, and add the %
character (with space) after the formatted value:
value = 0.25
Formatted = "My profit is {:.0f} %."
print(Formatted.format(value * 100))
If your string template doesn't need to by dynamic, you can combine the multiplication with the string format by using an f-string, which lets you use an expression inside of placeholders:
value = 0.25
print(f"My profit is {value * 100:.0f} %.")
you can use .replace()
Formatted = "My profit is {:.0%}."
print(Formatted.format(0.25).replace('%', ' %'))
or
formatted = "My profit is {:.0%}.".format(0.25).replace('%', ' %')
print(formatted)
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.