Python 3 makes the print function more explicit compared to Python 2.

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:

……

阅读全文