Fixed digits after decimal with f-strings
Include the type specifier in your format expression: >>> a = 10.1234 >>> f'{a:.2f}’ ‘10.12’
Include the type specifier in your format expression: >>> a = 10.1234 >>> f'{a:.2f}’ ‘10.12’
Here’s a complete “Ideal 2”. It’s not an f-string—it doesn’t even use f-strings—but it does as requested. Syntax exactly as specified. No security headaches since we are not using eval(). It uses a little class and implements __str__ which is automatically called by print. To escape the limited scope of the class we use the … Read more
To answer your first question… .format just seems more sophisticated in many ways. An annoying thing about % is also how it can either take a variable or a tuple. You’d think the following would always work: “hi there %s” % name yet, if name happens to be (1, 2, 3), it will throw a … Read more