Python 3 print function usage summary

1. The digital output string, and

. 1 >>> Print ( " runoob " )   # output string 
2  runoob 
 . 3 >>> Print (100)             # output digital 
. 4 100
 . 5 >>> STR = ' runoob ' 
. 6 >>> Print (STR)             # output variables 
7  runoob
 . 8 >>> L = [1,2, ' A ' ]          # listing 
. 9 >>> Print (L)  
 10 [. 1, 2, 'a']  
11 >>> t = (1,2,'a')         # 元组
12 >>> print(t)  
13 (1, 2, 'a')  
14 >>> d = {'a':1, 'b':2}    # 字典
15 >>> print(d)  
16 {'a': 1, 'b': 2}

 

2. formatted output integer

Support parameter format, similar to the C language printf

1 >>>str = "the length of (%s) is %d" %('runoob',len('runoob'))
2 >>> print(str)
3 the length of (runoob) is 6

python string formatting symbols:

<tbody

    Symbol description
      %c  Formatting characters and their ASCII code
      %s  Format string
      %d  Integer format
      in%  Unsigned int format
      %O  Formatting unsigned octal
      %x  Unsigned hexadecimal format
      %X  Unsigned hexadecimal format (uppercase)
      %f  Format floating point numbers, the point precision may be designated
      %e  Floating-point format in scientific notation
      %E  Action with% e, floating point numbers formatted in scientific notation
      %g  And% f% e shorthand
      %G  % F% E and shorthand
      %p  With an address number of variables in hexadecimal format

Operator assist command format:

symbol Features
* Defined width or point precision
- Used as a left-justified
+ Is displayed in front of the positive plus sign (+)
<sp> Show spaces in front of positive numbers
# Display octal number with leading zeros ( '0'), in front of the display hexadecimal '0x' or '0X' (depending on use the 'x' or 'X-')
0 The figures show the front padding '0' instead of the default space
% '%%' outputs a single '%'
(where) Variable Mapping (dictionary parameter)
m.n. m is the minimum overall width of the display, n is the number of digits after the decimal point (if available)

3. formatted output in hexadecimal, decimal, octal integer

the X-% #  --- hex hex

% d #  --- dec Decimal

% O #  --- the OCT octal

1 >>> nHex = 0xFF
 2 >>> print ( " nHex =% x ndeci =% d,% o night = " % (nHex, nHex, nHex))
 3 nHex = f, ndeci = 255 = 377 nights

4. formatted output float (float)

. 1 >>> PI = 3.141592653  
 2 >>> Print ( ' % 10.3f ' % PI) # field width 10, the accuracy. 3   
. 3       3.142  
 . 4 >>> Print ( " PI =%. * F " % (. 3, PI) ) # a * reading field width or precision tuple from behind   
. 5 PI = 3.142  
 . 6 >>> Print ( ' % 010.3f ' % PI) # filled with zeros blank   
. 7 000,003.142  
 . 8 >>> Print ( ' % - 10.3f '% More) #Left-aligned   
. 9 3.142       
 10 >>> Print ( ' % F + ' % PI) # Display sign   
11 +3.141593

5. Wrap

print end of the line will automatically add a carriage return, if need enter, simply add a comma at the end of the print statement 

, You can change its behavior.
1 >>>for i in range(0,6):
2 ...     print (i,)
3 ... 
4 0
5 1
6 2
7 3
8 4
9 5

 

6. print does not wrap

In Python print default swap line:

1 >>>for i in range(0,3):
2 ...     print (i)
3 ... 
4 0
5 1
6 2
7 >>>

 

To wrap you should not write  print (i, end = '' )

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

 

Guess you like

Origin www.cnblogs.com/liuyanhang/p/11115807.html