Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print in one line dynamically

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for item in range(1,100):     print item 

The result is:

1 2 3 4 . . . 

How get this to instead look like:

1 2 3 4 5 ... 

Even better, is it possible to print the single number over the last number, so only one number is on the screen at a time?

like image 721
Pol Avatar asked Jul 14 '10 19:07

Pol


People also ask

How do you print only one line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do you print on the same line without space in Python?

To print without a new line in Python 3 add an extra argument to your print function telling the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') The next print function will be on the same line.

How do you print a line by line in Python?

Using end keyword # print each statement on a new line print("Python") print("is easy to learn.") # new line print() # print both the statements on a single line print("Python", end=" ") print("is easy to learn. ")


1 Answers

Change print item to:

  • print item, in Python 2.7
  • print(item, end=" ") in Python 3

If you want to print the data dynamically use following syntax:

  • print(item, sep=' ', end='', flush=True) in Python 3
like image 133
ewall Avatar answered Sep 27 '22 18:09

ewall