Python-- Detailed print function

Foreword

  • print () method for printing out

grammar

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

parameter

  • objects - complex, it represents a plurality of output objects. A plurality of output objects, need, separated.
  • sep - interval for a plurality of objects, the default is a space.
  • end - the end of what used to be set. The default is a newline character \ n, we can be replaced with other strings.
  • file - the file object to write.
  • flush - whether the output is cached usually depends on the file, but if the flush key parameter to True, the flow will be forced to refresh.

Examples

And a digital output string

>>>print("test")  # 输出字符串
test 
>>> print(100)            # 输出数字
100
>>> str = 'test'
>>> print(str)            # 输出变量
test
>>> L = [1,2,'a']         # 列表 
>>> print(L)  
[1, 2, 'a']  
>>> t = (1,2,'a')         # 元组
>>> print(t)  
(1, 2, 'a')  
>>> d = {'a':1, 'b':2}    # 字典
>>> print(d)  
{'a': 1, 'b': 2}

Formatted output integer

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

symbol string formatting python

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

Formatting operator assist command

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)

Formatted output 16 hex, decimal, octal integer

  • #% X - hex hex

  • #% D - dec Decimal

  • #% O - oct octal

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

Formatted output float (float)

>>>pi = 3.141592653  
>>> print('%10.3f' % pi) #字段宽10,精度3  
     3.142  
>>> print("pi = %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度  
pi = 3.142  
>>> print('%010.3f' % pi) #用0填充空白  
000003.142  
>>> print('%-10.3f' % pi) #左对齐  
3.142       
>>> print('%+f' % pi) #显示正负号  
+3.141593

print does not wrap

  • In Python print default line break
  • To wrap you should not write print (i, end = '')
>>>for i in range(0,3):
...     print(i, end = '' )
... 
012

Reference: Print function usage summary

发布了29 篇原创文章 · 获赞 19 · 访问量 1309

Guess you like

Origin blog.csdn.net/s1156605343/article/details/104845768