Francisco Vargas Francisco Vargas - 4 years ago 443
Python Question

Print with no new line python

I have checked out the possible duplicates nonetheless they are not working out for me.

What I am trying to do is rotate a numpy array and I want to see this as an animation in the terminal:

if __name__ == "__main__":
o = np.ones((10,10))
while True:
for i in xrange(361):
sys.stdout.write(repr(rot_position(o, i)))

Tried print followed by a comma and it does not work either. How can I make it always print on the same line (that works on python 2.7x)?

Answer Source

The python 2.x "print with no newline" is: print("sometext"),

But, if you're trying to do what I think you're trying to do, between your while and for, add print("\r"),

Here's an example of a "ticker" program that illustrates what I'm talking about. Different, but I think it's applicable:


import time
import sys

i = 0
hello = "hello world, goodbye universe "

while 1:

    cur = hello[i:] + hello[0:i]
    print("%s" % cur),

    i += 1
    if (i >= len(hello)):
        i = 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download