Python format() (2024)

The format() method returns a formatted value based on the specified formatter.

Example

value = 45

# format the integer to binarybinary_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 formattingdecimal_value = format(123, 'd')

print("Decimal Formatting:", decimal_value)

# binary formattingbinary_value = format(123, 'b')

print("Binary Formatting:", binary_value)

Output

Decimal Formatting: 123Binary 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.

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 options

AlignmentDescription
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 widthno_width = format(123, 'd')

print("Without specified width:", no_width)

# formatting number with a width of 10, right-alignedright_aligned = format(123, '>10d')

print("Right-aligned with width 10:", right_aligned)

# formatting number with a width of 10, left-alignedleft_aligned = format(123, '<10d')

print("Left-aligned with width 10:", left_aligned)

# formatting number with a width of 10, center-alignedcenter_aligned = format(123, '^10d')

print("Center-aligned with width 10:", center_aligned)

Output

Without specified width: 123Right-aligned with width 10: 123Left-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 '+': +123Positive 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 numberprecision_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:

Python format() (2024)
Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6444

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.