Find knowledge base article(s) by searching for keywords in the title e.g. type linux in the search box below
Find knowledge base article(s) by browsing the subject categories of articles
Technology quick references, cheatsheets, user manuals etc.
Shop Online through ShopifyLite
Tutorials on various IT applications.
Search Title    (UL:0 |SS:f)

Scripting >> Python >> Examples >> How to do formatted print

Note:

print is a statement in Python 2.x
print is a function in Python 3.x => must use parentheses

 

Use the string.format() method to format the string for printing

Version

Example 1 : without length or type format specifier
Python 2.6 >>>print "{0} + {1} = {2}".format('1','2','3')
1 + 2 = 3
Python 2.7

>>>print "{0} + {1} = {2}".format('1','2','3')
1 + 2 = 3

or

>>>print "{} + {} = {}".format('1','2','3')
1 + 2 = 3

Python 3.6

>>> print("{0} + {1} = {2}".format('1','2','3'))
1 + 2 = 3

or

>>> print("{} + {} = {}".format('1','2','3'))
1 + 2 = 3

 

Version Example 2: with padding, length and type format specifier
Python 2.6 >>>print "{0:02d} + {1:02d} = {2:02d}".format(1,2,3)
01 + 02 = 03
 
Python 2.7

>>>print "{0:02d} + {1:02d} = {2:02d}".format(1,2,3)
01 + 02 = 03

or

>>>print "{:02d} + {:02d} = {:02d}".format(1,2,3)
01 + 02 = 03

Python 3.6

Note: print was a statement in 2.x, but it's a function in 3.x

>>>print("{:02d} + {:02d} = {:02d}".format(1,2,3))
01 + 02 = 03

or

>>>print("{0:02d} + {1:02d} = {2:02d}".format(1,2,3))
01 + 02 = 03

 

Version Example 3: Using keyword (named) arguments
Python 2.6 >>> print("{name} lives in {address}".format(name="Joe",address="California"))
Joe lives in California
>>>
 
Python 2.7
Python 3.6

 

 

Version Example 4: using formatted string literals (f or F prefix) - a.k.a f-strings
> Python 3.6

>>> name="Joe"
>>> address="Main Street"
>>> print(f"{name} lives in {address}")
Joe lives in Main Street
>>> print(F"{name} lives in {address}")
Joe lives in Main Street

 

 

Version Example 5: using formatted string literals with expressions within placeholder(s)
> Python 3.6 >>> a,b=3,4
>>> print(f"{a} to the power of {b} = {a**b}")
3 to the power of 4 = 81

 

 

Number format specifiers

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)
n Same as 'd'. Except it uses current locale setting for number separator
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.

 

OTHERS

# default arguments
>>> print("Hello {}, your balance is {}.".format("Adam", 230.2346))
Hello Adam, your balance is 230.2346.

# positional arguments
>>> print("Hello {0}, your balance is {1}.".format("Adam", 230.2346))
Hello Adam, your balance is 230.2346.

# keyword arguments

>>> print("Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346))
Hello Adam, your balance is 230.2346.


# mixed arguments

>>> print("Hello {0}, your balance is {blc}.".format("Adam", blc=230.2346))
Hello Adam, your balance is 230.2346.



# integer arguments
>>> print("The number is:{:d}".format(123))
The number is:123

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

# octal, binary and hexadecimal format
>>> print("bin: {0:b}, oct: {0:o}, hex: {0:x}".format(12))
bin: 1100, oct: 14, hex: c



# integer numbers with minimum width
>>> print("{:5d}".format(12))
   12

# padding for float numbers
>>> print("{:8.3f}".format(12.2346))
  12.235

# integer numbers with minimum width filled with zeros
>>> print("{:05d}".format(12))
00012

# padding for float numbers filled with zeros
>>> print("{:08.3f}".format(12.2346))
0012.235



# signed number, show the + sign
>>> print("{:+f} {:+f}".format(12.23, -12.23))
+12.230000 -12.230000

# signed number, show the - sign only
>>> print("{:-f} {:-f}".format(12.23, -12.23))
12.230000 -12.23000

# signed number, show space for + sign
>>> print("{: f} {: f}".format(12.23, -12.23))
 12.230000 -12.230000



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

# float numbers with center alignment
>>> print("{:^10.3f}".format(12.2346))
  12.235

# integer left alignment filled with zeros
>>> print("{:<05d}".format(12))
12000

# float numbers with center alignment
>>> print("{:=8.3f}".format(-12.2346))
- 12.235




 

[ © 2008-2021 myfaqbase.com - A property of WPDC Consulting ]