smart
smart

Reputation: 161

Adapting ‘print’ call to Python 2.4.3

I tested the following code with an online Python 3.x compiler but want a similar one to work on my 2.4.3 compiler:

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!')

How can I make a similar code work for Python 2.4.3?

Upvotes: 0

Views: 544

Answers (1)

Mike Müller
Mike Müller

Reputation: 85442

Use the print statement with trailing comma to suppress the newline:

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!'

Alternatively, write to sys.stdout directly:

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!')

This works in Python 2 and 3 alike.

Upvotes: 2

Related Questions