Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to execute multiple statemens while performing them in one line, like this:

import time
print ("Ok, I know how to write programs in Python now.")
time.sleep(0.5)
print (".") # This should print on the same line as the previous print statement.
time.sleep(0.5)
print (".") # ... As should this one

... So the output should be:

Ok, I know how to write programs in Python now.*.*.

*Waits .5 seconds

share|improve this question
    
You can call sys.stdout.write(). See this question: stackoverflow.com/questions/4499073/…. –  Kalle Elmér Jul 16 '12 at 13:54
4  
As this is a purely python related question and can be better served in other SE site, (such as Stack Overflow), I see this as off-topic here. –  Anwar Shah Jul 16 '12 at 14:16
    
Sorrt @KalleElmér, I'be jsut started learning Python about a month ago - could anyone give an expleanation on how to accomplish my example in the question? –  Marco Scannadinari Jul 16 '12 at 15:27
add comment

4 Answers

up vote 3 down vote accepted

In Python 2, the print statement automatically adds a line feed, so you need to use sys.stdout.write() instead. You will also have to import sys. The code you have written should look like this instead:

import time
import sys
sys.stdout.write("Ok, I know how to write programs in Python now.")
time.sleep(0.5)
sys.stdout.write(".")
time.sleep(0.5)
sys.stdout.write(".")

In Python 3, print is a function accepting keyword arguments. You can use the end keyword argument to specify what should be placed after your string. By default it's a new line character, but you can change it to an empty string:

import time
print("Ok, I know how to write programs in Python now.", end='')
time.sleep(0.5)
print(".", end='')
time.sleep(0.5)
print(".", end='')

Also, remember that streams are buffered, so it's better if you flush them:

import time
import sys
print("Ok, I know how to write programs in Python now.", end='')
sys.stdout.flush()
time.sleep(0.5)
print(".", end='')
sys.stdout.flush()
time.sleep(0.5)
print(".", end='')
sys.stdout.flush()
share|improve this answer
add comment

You should be able to do it with the end="" syntax also.

print("this ",end="")
print("will continue on the same line")
print("but this wont")

Will return

this will continue on the same line
but this wont

so the following would work as well.

import time
print ("Ok, I know how to write programs in Python now.",end="")
time.sleep(0.5)
print (".",end="") # This should print on the same line as the previous print statement.
time.sleep(0.5)
print (".") # ... As should this one
share|improve this answer
add comment

Isn't this simpler?:

import time
print ("Ok, I know how to write programs in Python now."),
time.sleep(0.5)
print ("."), # This should print on the same line as the previous print statement.
time.sleep(0.5)
print (".") # ... As should this one
share|improve this answer
    
This does not work in Python3 –  Takkat Dec 29 '12 at 12:46
    
Oh, thanks! This is what happens when you don't update ;-)! –  Jobin Dec 29 '12 at 15:08
    
I don't think you should space you parentheses like that either ;), and you don't get an update from Python2.7 to 3.* - you have to install it with $ su -, # apt-get install python3, because Python2.7 is still used a lot. 12.10 should have switched though... –  Marco Scannadinari Mar 3 '13 at 19:31
add comment

Can this also be done with inputs?

print("THIS IS A TEST AREA")
print()
print("TETST OF SAME LINE INTERACTION")
print("X:  ", end="") #This works Fine
input("")
time.sleep(0.5)  #This however dew to python3 wont?
print("     STAR")

This output looks like so...

THIS IS A TEST AREA

TETST OF SAME LINE INTERATION
X:  
     STAR
>>> 
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.