1. Output strings and numbers

>>>print("example") # Output string example >>> print(100) # Output number 100 >>> str = 'example' >>> print(str) # Output variable example >>> 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. Format output integer

Supports parameter formatting, similar to printf in C language

>>>str = "the length of (%s) is %d" %('example',len('example')) >>> print(str) the length of (example) is 6

Python string formatting symbols:

Symbol Description
      %cFormat character and its ASCII code
      %sFormat string
      %dFormat integer
      %uFormat unsigned integer
      %oFormat unsigned octal number
      %xFormat unsigned hexadecimal number
      %XFormat unsigned hexadecimal number (uppercase)
      %fFormat floating-point number, you can specify precision after decimal point
      %eFormat floating-point number in scientific notation
      %ESame as %e, format floating-point number in scientific notation
      %gShorthand for %f and %e
      %GShorthand for %f and %E
      %pFormat variable address in hexadecimal

Formatting operator auxiliary directives:

SymbolFunction
*Define width or decimal point precision
-Used for left alignment
+Display plus sign ( + ) before positive numbers
<sp>Display space before positive numbers
#Display zero ('0') before octal numbers, display '0x' or '0X' before hexadecimal (depending on whether 'x' or 'X' is used)
0Fill with '0' instead of default spaces in front of displayed numbers
%'%%' outputs a single '%'
(var)Mapping variables (dictionary parameters)
m.n.m is the minimum total width to display, n is the number of digits after the decimal point (if available)

3. Format output 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. Format output 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 the following tuple pi = 3.142 >>> print('%010.3f' % pi) # Fill blanks with 0 000003.142 >>> print('%-10.3f' % pi) # Left align 3.142 >>> print('%+f' % pi) # Display positive and negative signs +3.141593

5. Automatic line break

print automatically adds a carriage return at the end of the line. If a carriage return is not needed, just add a comma at the end of the print statement,And set the separator parameter end to change its behavior.
>>>for i in range(0,6): ... print(i) ... 0 1 2 3 4 5 >>> for i in range(0,6): ... print(i, end=" ") ... 0 1 2 3 4 5

6. print without line break

In Python, print is line-breaking by default:

>>>for i in range(0,3): ... print (i) ... 0 1 2 >>>

To avoid line break, you should write it asprint(i, end = '' )

>>>for i in range(0,3): ... print(i, end = '' ) ... 012