我使用在线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?
答案 0 :(得分:2)
使用带有逗号逗号的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。