The format()
method returns a formatted value based on the specified formatter.
Example
value = 45 # format the integer to binary binary_value = format(value, 'b') print(binary_value) # Output: 101101
format() Syntax
format(value, format_spec)
format() Parameters
The function takes two parameters:
- value - the data we want to format
- format_spec - the specification on how we want to format the data
format() Return Value
The format()
function returns a value in the string representation of the desired format.
Example: Numeric Formatting Using format()
# 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
Here, format(123, 'd')
and format(123, 'b')
converts the integer 123 to its decimal and binary string representation respectively.
Note: We have used format specifiers, d
for decimal and b
for binary. To learn more about format types, visit Format Types.
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 options
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 |
Let's look at an example.
# formatting numbers without specifying width no_width = format(123, 'd') print("Without specified width:", no_width) # formatting number with a width of 10, right-aligned right_aligned = format(123, '>10d') print("Right-aligned with width 10:", right_aligned) # formatting number with a width of 10, left-aligned left_aligned = format(123, '<10d') print("Left-aligned with width 10:", left_aligned) # formatting number with a width of 10, center-aligned center_aligned = format(123, '^10d') print("Center-aligned with width 10:", center_aligned)
Output
Without specified width: 123 Right-aligned with width 10: 123 Left-aligned with width 10: 123 Center-aligned with width 10: 123
Here,
format(123, 'd')
- number is formatted without a specified width.format(123, '>10d')
- number is right-aligned within a 10-character width with extra space on the left.format(123, '<10d')
- number is left-aligned within a 10-character width with extra space on the right.format(123, '^10d')
- number is centered within a 10-character width with extra space evenly distributed on both sides.
Example: Number Formatting With Sign
# using '+' 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
In the above example, we formatted the numbers 123 and -123 using each of the sign options.
The sign option in formatting controls the display of the sign for the numbers. Here, +
indicates that both positive and negative numbers will show their signs.
Note: The sign option -
indicates only negative numbers will show their sign and ' '
will show a space for positive numbers and a minus for negative numbers.
Example: Number Formatting With Precision
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
Here, .2f
means two digits will be shown after the decimal point.
Also Read: