python built-in function print ()

Taken https://www.cnblogs.com/Dake-T/p/7376779.html

English documentation:

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

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used withbinary mode file objects. For these, use file.write(...) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

Function information table

 

Function prototype

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

 

 

Parameter Description

 

 

 

 

 

 

 

value Content to be printed
... You can enter multiple content

sep

Separator, default spaces;

end

This parameter specifies the string added at the end of the output, the default is a newline;

file

Definition of the output stream file, a standard default output sys.stdout system, redefined to other documents;

flush

Whether immediately the output to a stream file, without cache, the default is False.

return value

<Class 'NoneType'> returns a corresponding output.

Function Description

Print the corresponding content.

Formatted output print function

Formatted output:

Start flag conversion specifier: 1) Character%

2) conversion flags: - for left-aligned; + indicates the value before the conversion to add the sign; "" (blank character) expressed reservations about the space before positive number; 0 represents the number of bits is not enough if the conversion value is padded with 0

3) Minimum field width: the converted string should have at least the value of a specified width. * If it is, the width will be read from the value tuple.

4) followed by precision value point '.': If the conversion is a real number, the accuracy value is now shown after the decimal point. If the conversion is a string, then the number means the maximum field width. * If it is, then the accuracy of the read-out from the tuple

5) conversion type String Format

meaning

d,i

Decimal integer with a sign

O

Unsigned octal

in

Unsigned Decimal

x

Unsigned hexadecimal (lowercase)

X

Unsigned hexadecimal (uppercase)

e

Float (lowercase) represented by the scientific notation

E

Float (capital) expressed in scientific notation

f,F

Decimal floating point

g

If the index is greater than or less than -4 precision value and the same e, and f are the same as the case of the other

G

如果指数大于-4或者小于精度值则和E相同,其他情况和F相同

C

单字符(接受整数或者单字符字符串)

r

字符串(使用repr转换任意python对象)

s

字符串(使用str转换任意python对象)

 

范例1:基本的打印输出(Python 3.6.2 shell 环境)

 
1 >>> print(1,'2',[3.40,'5'],(6,[7,8],'9'))            #参数缺省
2 1 2 [3.4, '5'] (6, [7, 8], '9')
3 >>> print(1, '2', 3.00, sep = '|', end = '\nline2')  #使用'|'作为分隔符,'\nline2'为结束符
4 1|2|3.0                                             
5 line2
6 >>> print(1, '2', 3.00, sep = '', end = '')          #这里需要注意"'2'"输出后为"2"
7 123.0
 

 

 范例2:通过更改file参数打印内容到文件(Python 3.6.2 shell 环境)

1 >>> with open(r'G:\temp.txt', 'w') as demo:  
2 print(1, 2, 3, sep = ',', end = '\n', file = demo)
3 
4 
5 >>>

  G盘下被新建txt文档’temp.txt’,其内容为:

  1,2,3

  line2

 

范例3:格式化输出(Python 3.6.2 Shell 环境)

 
 1 >>> pi = 3.141592653
 2 >>> print('%f' % pi)                                   #默认愿长度为宽度,保留小数点后六位
 3 3.141593
 4 >>> print('%+f' % pi)                                  #显示正负号  
 5 +3.141593
 6 >>> print('%4f' % pi)                                  #宽度设小不会丢失精度
 7 3.141593
 8 >>> print('%10.3f' % pi)                               #宽度10,保留小数点后2位,右对齐
 9 *****3.142                                                #用*表示空格
10 >>> print('%-10.3f' % pi)                              #宽度10,保留小数点后2位,左对齐
11 3.142*****
12 >>> print('%010.3f' % pi)                              #用0填充空白
13 000003.142  
14 >>> print('%e' % pi)                                    #科学计数法格式化输出
15 3.141593e+00
16 >>> print('%.2E' % (pi * 10000))
17 3.14E+04
18 >>> print('a%cc' % 'b')                                #单字符格式化输出
19 abc
20 >>> print('%c' % 'abc')                                #单字符格式输入多字符时的报错信息
21 Traceback (most recent call last):
22   File "<pyshell#12>", line 1, in <module>
23     print('%c' % 'abc')
24 TypeError: %c requires int or char
25 >>> print('a%sd' % 'bc')                               #格式化输出字符串
26 abcd
27 >>> print('a%rd' % 'bc')                               #格式化输出字符串,保留“’’”
28 a'bc'd
29 >>>
 

 

若您喜欢这篇文章请点个赞再走吧。欢迎转载,但请注明出处。由于水平有限,如果有不完善的地方还请在评论区中指正。有疑问欢迎在评论区中一起探讨。

Guess you like

Origin www.cnblogs.com/blogzyq/p/11301021.html