Python format() Function
The format
method conerts a value to a string using the specified format.
For example, ".2f" means, format the value to two numbers past the decimal
point.
Syntax
Python
Copy Code
format(object, spec)
Parameters
Parameter | Description |
---|---|
object |
Required. The object to be converted to a string |
spec |
Required. The format specification. |
Example
Python
Copy Code
price = 49 formattedPrice = format(price, '.2f') print(formattedPrice)
Output
49.00