print的几种方法

1. 输出参数

1
2
3
4
5
6
7
8
9
10
11
12
a = 2
b = "hello"
print("output: ", a, b) # 1
print("output: %d %s" % (a, b)) # 2
print("output: {} {}".format(a, b)) # 3
print(f"output: {a} {b}") # 4
'''
output: 2 hello
output: 2 hello
output: 2 hello
output: 2 hello
'''

2. flush=True

print(xxx, flush=True)

print的输出是有条件的,在某些特定场合print语句结束之后是不会打印输出的,flush为True就是不论满不满足条件,只要print语句结束就可以立即输出