Python format() function
Example
Format the number 0.5 as a percentage value:
x = format(0.5, '%')
Run Instance
Definition and Usage
The format() function formats the specified value into the specified format.
Syntax
format(value, format)
Parameter Value
Parameter |
Description |
value |
Any value in any format. |
format |
The format you want to format the value to.
Valid values:
- '<' - Left-aligned result (within available space)
- '>' - Right-aligned result (within available space)
- '^' - Center-aligned result (within available space)
- '=' - Place the sign at the leftmost position
- '+' - Use plus sign to indicate whether the result is positive or negative
- '-' - Negative sign is only used for negative values
- ' ' - Use space before positive numbers
- ',' - Use comma as a thousand separator
- '_' - Use underscore as a thousand separator
- 'b' - Binary format
- 'c' - Convert the value to the corresponding unicode character
- 'd' - Decimal format
- 'e' - Scientific format, using lowercase e
- 'E' - Scientific format, using uppercase E
- 'f' - Fixed-point number format
- 'F' - Fixed-point number format, uppercase
- 'g' - General format
- 'G' - General format (uses uppercase E for scientific notation)
- 'o' - Octal format
- 'x' - Hexadecimal format, lowercase
- 'X' - Hexadecimal format, uppercase
- 'n' - Numeric format
- '%' - Percentage format
|
More Examples
Example
Format 255 as a hexadecimal value:
x = format(255, 'x')
Run Instance