春江暮客

春江暮客的个人学习分享网站

Summary of Python3 print function usage

2019-06-01 Miscellaneous
Summary of Python3 print function usage

Python 3 makes print a standard function, which makes its behavior clearer and easier to control than the older Python 2 statement form.

1. Outputting Strings and Numbers

print("runoob") # Outputs string runoob

print(100) # Outputs number 100

str = 'runoob'

print(str) # Outputs variable runoob

L = [1,2,'a'] # List

print(L) [1, 2, 'a']

t = (1,2,'a') # Tuple

print(t) (1, 2, 'a')

d = {'a':1, 'b':2} # Dictionary

print(d) {'a': 1, 'b': 2}

2. Formatted Integer Output

Supports parameter formatting, similar to C language’s printf.

str = "the length of (%s) is %d" %('runoob',len('runoob'))

print(str) # the length of (runoob) is 6

Python String Formatting Symbols:

** Symbol** Description
%c Formats character and its ASCII code
%s Formats string
%d Formats signed decimal integer
%u Formats unsigned decimal integer
%o Formats unsigned octal number
%x Formats unsigned hexadecimal number (lowercase)
%X Formats unsigned hexadecimal number (uppercase)
%f Formats floating-point number, precision can be specified after decimal point
%e Formats floating-point number in scientific notation (lowercase ’e')
%E Same as %e, formats floating-point number in scientific notation (uppercase ‘E’)
%g Shorthand for %f and %e
%G Shorthand for %f and %E
%p Formats variable’s address in hexadecimal

Formatting Operator Auxiliary Directives:

** Symbol** Function
* Defines width or decimal precision
- Used for left alignment
+ Displays a plus sign (+) before positive numbers
(space) Displays a space before positive numbers
# Displays ‘0’ before octal numbers, ‘0x’ or ‘0X’ before hexadecimal numbers (depending on ‘x’ or ‘X’)
0 Pads the number with ‘0’ instead of default spaces
% ‘%%’ outputs a single ‘%’
(var) Maps variable (dictionary parameter)
m.n. m is the minimum total width displayed, n is the number of digits after the decimal point (if available)

3. Formatted Output of Hexadecimal, Decimal, Octal Integers

#%x — hex hexadecimal

#%d — dec decimal

#%o — oct octal

nHex = 0xFF

print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex)) # nHex = ff,nDec = 255,nOct = 377

4. Formatted Output of Floating-Point Numbers (float)

pi = 3.141592653

print('%10.3f' % pi) # field width 10, precision 3 -> 3.142

print("pi = %.*f" % (3,pi)) # use * to read field width or precision from trailing tuple -> pi = 3.142

print('%010.3f' % pi) # pad with 0 -> 000003.142 >>> print('%-10.3f' % pi) # left-align -> 3.142

print('%+f' % pi) # display sign -> +3.141593

5. Automatic Line Break

In Python 3, print() adds a newline at the end by default, so repeated calls usually appear on separate lines.

for i in range(0, 3):
	print(i)

0
1
2

6. Print Without Newline

If you do not want a newline, the Python 3 way is to use the end parameter instead of the old Python 2 trailing-comma style.

for i in range(0, 3):
	print(i, end=" ")

0 1 2

If you want to change the separator between multiple printed values, you can also use sep:

print("Python", 3, "print", sep=" - ")
# Python - 3 - print

友情链接

其它