print 的几种方法
python
本文字数:128 字 | 阅读时长 ≈ 1 min

print 的几种方法

python
本文字数:128 字 | 阅读时长 ≈ 1 min

1. 输出参数

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 语句结束就可以立即输出

4月 06, 2025
3月 10, 2025
12月 31, 2024