Python's print function syntax format and detailed explanation of each parameter

1. print syntax format

The print() function has rich functions, and the detailed syntax format is as follows:

print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

By default, the value is printed to a stream or to sys.stdout.

Optional keyword arguments:

  • file: file-like object (stream); defaults to the current sys.stdout.
  • sep: A string to insert between values, defaults to spaces.
  • end: A string to append after the last value, defaults to a newline.
  • flush: Whether to force the stream to be flushed.

2. sep optional keyword argument

The sep parameter can insert a string between values, the default value is a space.

like:

print('1','2','3','4',sep = "插入")

Output result:

1插入2插入3插入4

3. file optional keyword parameter

        The default value of the file parameter is sys.stdout, which represents the system standard output, that is, the screen. We can make the print() function output to a specific file by changing this parameter.

like:

f =  open(r"F:\text.txt","w")    # 打开文件,以便写入
print('test',file =)    # 输出到文件
f.close()    # 关闭文件

After running, you can see that the test is output to the text.txt file.


4. end optional keyword parameter

        The end parameter defaults to "\n" (newline character) . If you want to output other strings after the output of the print() function, you can reset the end parameter.

like:

print('1','2',end = "最后")

Output result:

1 2最后

After running, we can see that the output of the print() function will not wrap, and "last" will be appended after the last value.

The end optional keyword argument also has a method for automatic line wrapping

        print will automatically add a carriage return at the end of the line. If no carriage return is required, just add a comma at the end of the print statement  and  set the separator parameter end to change its behavior.

6. print does not wrap

In Python, print is newline by default:

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

If you don't want a new line, you should write it as  print(i, end = '' )

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

5. flush optional keyword parameter

The flush parameter is used to control the output cache. Generally, in order to obtain better performance, keep it as False.


Python string formatting symbols:

    symbol describe
      %c  Formatting characters and their ASCII codes
      %s  format string
      %d  format integer
      %u  format unsigned integer
      %o  Format an unsigned octal number
      %x  Format an unsigned hexadecimal number
      %X  format unsigned hexadecimal number (uppercase)
      %f  Format floating point numbers, you can specify the precision after the decimal point
      %e  Format floating point numbers in scientific notation
      %E  Same as %e, format floating point numbers in scientific notation
      %g  Shorthand for %f and %e
      %G  Shorthand for %f and %E
      %p  format the address of a variable with a hexadecimal number

Formatting operator auxiliary instructions:

symbol Function
* Define width or decimal point precision
- for left alignment
+ Display a plus sign ( + ) in front of positive numbers
<sp> display spaces before positive numbers
# Display zero ('0') in front of octal numbers, '0x' or '0X' in front of hexadecimal numbers (depending on whether 'x' or 'X' is used)
0 Displayed numbers are preceded by '0' instead of the default space
% '%%' outputs a single '%'
(was) map variable (dictionary parameter)
m.n. m is the minimum total width of the display, n is the number of digits after the decimal point (if available)

Formatted 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

Formatted output floating point number (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

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131031390