将“打印”调用调整为 Python 2.4.3

Adapting ‘print’ call to Python 2.4.3

我使用在线 Python 3.x 编译器测试了以下代码,但希望类似的代码能够在我的 2.4.3 编译器上运行:

import sys, time

print('I am about to show you something _\b', end='')
sys.stdout.flush() # Make sure above string gets printed in time
time.sleep(2)
print('THIS!')

如何使类似的代码适用于 Python 2.4.3?

使用带有尾随逗号的 print 语句来取消换行:

import time
import sys

print 'I am about to show you something _\b',
sys.stdout.flush() # Make sure above string gets printed in time
time.sleep(2)
print 'THIS!'

或者,直接写入 sys.stdout

sys.stdout.write('I am about to show you something ')
sys.stdout.flush() # Make sure above string gets printed in time
time.sleep(2)
sys.stdout.write('THIS!')

这在 Python 2 和 3 中同样有效。