python学习记录1-print
时间:2022-03-29 01:54
print()函数学习
print() 方法用于打印输出,它的函数语法如下:
print(*objects, sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False)
参数的具体含义如下:
objects --表示输出的对象。输出多个对象时,需要用逗号(,)分隔。
sep -- 用来间隔多个对象,参数表示每个对象间隔的元素,默认参数空格。
end -- 用来设定以什么结尾。默认值是换行符 \n,我们可以换成其他字符。
file -- 要写入的文件对象。sys.stdout参数指向控制台。
flush -- 参数为True时把缓冲区的数据强行输出,清空缓冲区
print具有如下特点:
1.print函数可以在直接输出任何数据变量,包括数值型,布尔型,列表变量,字典变量等
>>>print("hello world!", 11) hello world! 11
>>>print([x for x in range(1, 10, 2)])
[1, 3, 5, 7, 9]
2.格式化输出--"%"、"\"和format用法
[1] % 的用法
(1)整数输出
符号 | 含义 |
b | 二进制输出 |
c | 字符。在打印之前将整数转换成对应的Unicode字符串。 |
d | 十进制输出 |
o | 八进制输出 |
x | 十六进制输出 |
e | 幂符号。用科学计数法打印数字。用‘e‘表示幂 |
g | 一般格式。将数值以fixed-point格式输出。当数值特别大的时候,用幂形式打印。 |
h | 数字。当值为整数时和‘d‘相同,值为浮点数时和‘g‘相同。不同的是它会根据区域设置插入数字分隔符。 |
% | 百分数。将数值乘以100然后以fixed-point(‘f‘)格式打印,值后面会有一个百分号。 |
>>>print("{:b}".format(30)) 11110 >>>print("{:c}".format(97)) a >>>print("{:d}".format(30)) 30 >>>print("{:o}".format(30)) 36 >>>print("{:x}".format(3000)) 1e >>>print("{:e}".format(30)) 3.000000e+03 >>>print("{:g}".format(30.21)) 30.21 >>>print("{:f}".format(30)) 30.000000 >>>print("{:n}".format(30)) 30 >>>print("{:%}".format(30)) 3000.000000%