Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print 'tight' dots horizontally in python?

I have a program which prints out its progress to the console. Every 20 steps, it prints the number of steps like 10 20 30, etc. but within this, it prints a dot. This is printed using the print statement with a comma at the end (python 2.x)

        if epoch % 10 == 0:
            print epoch,
        else:
            print ".",

Unfortunately, I noticed that the dots are printed apart from each other, like this:

0 . . . . . . . . . 10 . . . . . . . . . 20 . . . . . . . . . 30

I want this to be tighter, as follows:

0.........10.........20.........30

In visual basic language, we can get this form if we add a semicolon to the end of the print statement instead of the comma. Is there a similar way to do so in Python, or a walkthrough to get tighter output?

Note:

With all thanks and respect to all who replied, I noticed that some of them considered the change in 'epoch' happens in a timely manner. Actually, it is not, as it happens after finishing some iterations, which may take from a fraction of second to several minutes.

like image 999
Mohammad ElNesr Avatar asked Aug 24 '16 12:08

Mohammad ElNesr


People also ask

How to start from the end of the line in Python?

We start from the end of the line using positive indices, respectively, the start parameter of the range function -len (variable) -1. -1 because the length of the string is always 1 more than the index of its last element.

What is tight_layout in Matplotlib?

matplotlib.pyplot.tight_layout () Function The tight_layout () function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding. Syntax: matplotlib.pyplot.tight_layout (pad=1.08, h_pad=None, w_pad=None, rect=None)

How do you walk through a string in Python?

We will put a loop in the function that will "walk" through each of the elements of the string. We start from the end of the line using positive indices, respectively, the start parameter of the range function -len (variable) -1. -1 because the length of the string is always 1 more than the index of its last element.


3 Answers

If you want to get more control over the formatting then you need to use either:

import sys
sys.stdout.write('.')
sys.stdout.flush()  # otherwise won't show until some newline printed

.. instead of print, or use the Python 3 print function. This is available as a future import in later builds of Python 2.x as:

from __future__ import print_function
print('.', end='')

In Python 3 you can pass the keyword argument flush:

print('.', end='', flush=True)

which has the same effect as the two lines of sys.stdout above.

like image 115
solidpixel Avatar answered Oct 19 '22 09:10

solidpixel


import itertools
import sys
import time


counter = itertools.count()


def special_print(value):
    sys.stdout.write(value)
    sys.stdout.flush()


while True:
    time.sleep(0.1)
    i = next(counter)
    if i % 10 == 0:
        special_print(str(i))
    else:
        special_print('.')
like image 1
turkus Avatar answered Oct 19 '22 07:10

turkus


Here's a possible solution:

import time
import sys

width = 101

for i in xrange(width):
    time.sleep(0.001)
    if i % 10 == 0:
        sys.stdout.write(str(i))
        sys.stdout.flush()
    else:
        sys.stdout.write(".")
        sys.stdout.flush()

sys.stdout.write("\n")
like image 1
BPL Avatar answered Oct 19 '22 08:10

BPL