Python format() Function
The format() function formats a specified value into a specified format.
Syntax
format(value, format)
format() Parameters
Python format() function parameters:
| Parameter | Condition | Description |
|---|---|---|
value | Required | A number or a string that can be converted into a floating point number |
format_string | Required | The format you want to format the value into. Legal values: <: Left aligns the result (within the available space)>: Right aligns the result (within the available space)^: Center aligns the result (within the available space)=: Places the sign to the left most position+: Use a plus sign to indicate if the result is positive or negative-: Use a minus sign for negative values only : Use a leading space for positive numbers,: Use a comma as a thousand separator_: Use a underscore as a thousand separatorb: Binary formatc: Converts the value into the corresponding unicode characterd: Decimal formate: Scientific format, with a lower case eE: Scientific format, with an upper case Ef: Fix point number formatF: Fix point number format, upper caseg: General formatG: General format (using a upper case E for scientific notations)o: Octal formatx: Hex format, lower caseX: Hex format, upper casen: Number format%: Percentage format |
format() Return Value
Python format() function returns a value in the string representation of the desired format.
Examples
Example 1: number formatting using format() function
Example of decimal and binary formatting:
# decimal formatting
decimal_value = format(123, 'd')
print("Decimal Formatting:", decimal_value)
# binary formatting
binary_value = format(123, 'b')
print("Binary Formatting:", binary_value)
output
Decimal Formatting: 123
Binary Formatting: 1111011
Example 2: number formatting with sign using format() function
Let's use + and - format specifier:
positive_with_plus = format(123, '+')
positive_with_minus = format(123, '-')
print("Positive with '+':", positive_with_plus)
print("Positive with '-':", positive_with_minus)
output
Positive with '+': +123
Positive with '-': 123
Example 3: number formatting with precision using format() function
Precision allows us to define the number of digits to be shown after a decimal point.
For example, set precision to 2 for floating-point number:
precision_value = format(123.4567, '.2f')
print(precision_value) # Output: 123.46
output
123.46
Example 4: number formatting with alignment and width
Alignment in formatting refers to the way a number is positioned or placed within the available space, determining where it starts and ends in relation to that space.
It controls how the number is visually presented within a designated area.
| Alignment | Description |
|---|---|
< left-align | Aligns the output string to the left |
> right-align | Aligns the output string to the right |
^ center-align | Aligns the output string to the center of the remaining space |
For example, format number without specifying width:
no_width = format(123, 'd')
print("Without specified width:", no_width)
Without specified width: 123
For example, format number with a width of 10, right-aligned:
right_aligned = format(123, '>10d')
print("Right-aligned with width 10:", right_aligned)
Right-aligned with width 10: 123
For example, format number with a width of 10, left-aligned:
left_aligned = format(123, '<10d')
print("Left-aligned with width 10:", left_aligned)
Left-aligned with width 10: 123
For example, format number with a width of 10, center-aligned:
center_aligned = format(123, '^10d')
print("Center-aligned with width 10:", center_aligned)
Center-aligned with width 10: 123