100% found this document useful (1 vote)
67 views

Python - Number Formatting

The format() function allows formatting of numbers in various ways. Some key formatting specifiers include: d - Decimal integer, f - Fixed point number, e - Exponential notation, % - Percentage. Padding and alignment like <, ^, > can be used to control number positioning. Common uses include right aligning with {:5d}, centering with {:^10.3f}, and left filling zeros with {:<05d}.

Uploaded by

Fenny Todarmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
67 views

Python - Number Formatting

The format() function allows formatting of numbers in various ways. Some key formatting specifiers include: d - Decimal integer, f - Fixed point number, e - Exponential notation, % - Percentage. Padding and alignment like <, ^, > can be used to control number positioning. Common uses include right aligning with {:5d}, centering with {:^10.3f}, and left filling zeros with {:<05d}.

Uploaded by

Fenny Todarmal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Numbers formatting with format()

You can format numbers using the format specifier given below:
Type Meaning
d Decimal integer
c Corresponding Unicode character
b Binary format
o Octal format
x Hexadecimal format (lower case)
X Hexadecimal format (upper case)
e Exponential notation. (lowercase e)
E Exponential notation (uppercase E)
f Displays fixed point number (Default: 6)
F Same as 'f'. Except displays 'inf' as 'INF' and 'nan' as 'NAN'
g General format. Rounds number to p significant digits. (Default
precision: 6)
G Same as 'g'. Except switches to 'E' if the number is large.
% Percentage. Multiples by 100 and puts % at the end.
Simple number formatting
# integer arguments
print("The number is:{:d}".format(123))
#123

# float arguments
print("The float number is:
{:f}".format(123.4567898)) #123.4567898

# octal, binary and hexadecimal format


print("bin: {0:b}, oct: {0:o}, hex: {0:x}".format(12))
#bin: 1100, oct: 14, hex: c
Number formatting with padding for
int and floats
# integer numbers with minimum width
1 2
print("{:5d}".format(12))

# width doesn't work for numbers longer than padding


1 2 3 4
print("{:2d}".format(1234))

# padding for float numbers


print("{:8.3f}".format(12.2346))
1 2 . 2 3 5

# integer numbers with minimum width filled with zeros


print("{:05d}".format(12))0 0 0 1 2

# padding for float numbers filled with zeros


print("{:08.3f}".format(12.2346))
0 0 1 2 . 2 3 5
Number formatting for signed
numbers
# show the + sign
print("{:+f} {:+f}".format(12.23, -12.23))
# +12.230000 -12.230000

# show the - sign only


print("{:-f} {:-f}".format(12.23, -12.23))
# 12.230000 -12.230000

# show space for + sign


print("{: f} {: f}".format(12.23, -12.23))
# 12.230000 -12.23000
Number formatting with alignment
Type Meaning
< Left aligned to the remaining space
^ Center aligned to the remaining space
> Right aligned to the remaining space
= Forces the signed (+) (-) to the leftmost
position
Number formatting with alignment

# integer numbers with right alignment
1 2
print("{:5d}".format(12))

# float numbers with center alignment


1 2 . 2 3 5
print("{:^10.3f}".format(12.2346))

# integer left alignment filled with zeros


print("{:<05d}".format(12)) 1 2 0 0 0

# float numbers with center alignment


-
print("{:=8.3f}".format(-12.2346)) 1 2 . 2 3 5

You might also like