CKTIS / Python
format()
Converts a value to text using the formatting protocol implemented by its type.
Version: Python 3.14; repository image, production runtime not attested
Syntax
format(value, format_spec="", /)Converts a value to text using the formatting protocol implemented by its type.
Numeric padding and precision affect the resulting string while leaving the original numeric value unchanged.
Formatting dispatches to the type’s __format__ method and returns a string. In 07.2f the width seven is a minimum, zeroes fill the left side and two fractional digits are shown. The exactly representable tie 3.125 rounds to the even hundredth 3.12. The float still holds 3.125.
amount = 3.125
assert format(amount, "07.2f") == "0003.12" and amount == 3.125
assert format(12345, "02d") == "12345"
Examples
amount = 3.125
print(format(amount, "07.2f"))
print(amount)
Result
0003.12 3.125
Formatting dispatches to the type’s `__format__` method and returns a string. In 07.2f the width seven is a minimum, zeroes fill the left side and two fractional digits are shown. The exactly representable tie 3.125 rounds to the even hundredth 3.12. The float still holds 3.125.