>>> help(print)
Help on built-in function print in module builtins:
print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
语法格式:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
等价于
print(value1,value2,...,valuen,sep=' ', end='\n', file=sys.stdout, flush=False)
# sep 表示多个value之间的连接方式,默认空格
a = 1
b = 2
print(a,b,sep='#')
# end 表示print的结尾方式,默认为换行
for i in range(5):
print(i,end=' ')
# flush 输出时刷新缓存
格式 | 描述 |
---|---|
%d | 有符号的整数 |
%s | 字符串 |
%c | 字符及ASCII码 |
%o | 无符号八进制整数 |
%x/%X | 无符号十六进制整数 |
%e/%E | 浮点数,科学计数法 |
%f | 浮点数 |
%格式化字符串 用%匹配参数,注意个数一一对应
"%d%%" % 100---> 100%
print("%d" % 6688)
name = "liyue"
age = 21
print("His name is %s, his age is %d." % (name, age))
print("His name is", name, ", his age is ", age, '.')
print("His name is " + name + ", his age is " + str(age) + '.')# 用+连接时要注意数据类型
print("Name:{},age:{}".format('Tom', 18))#format带的参数与花括号一一对应
print("Name:{},age:{}, {address}".format('Tom', 18, address="Gongshu,Hangzhou"))
# {address}可放在任意位置,format会进行关键字映射。但format内的关键字参数需放在固定参数后方
print("第一个元素是:{0[0]}, 第二个元素是:{0[1]}, 第三个元素是:{0[2]}; \ # \ 为换行符
第四个元素:{1[0]},第五个元素:{1[1]},第六个元素为{0[2]}\
".format(
('www.', 'google.', 'com'), ('www.', 'baidu.'))) # 括号内可直接换行