0

When I print a strings character one by one in a line, space occur automatic. between them!!

import time
a="mayank"
for z in a:
    time.sleep(0.1)
    print z,

m a y a n k

But I want to print them without spaces between them!! Like:-

mayank

In python2.7

  • use `end` argument with print, say : `print(z, end='')`. I assume you are using `Python 3`. – Shubham Namdeo Jan 04 '17 at 02:56
  • 2
    Possible duplicate of [How to print in Python without newline or space?](http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space) – Adrian Jan 04 '17 at 03:03
  • 1
    Possible duplicate of [How to print one character at a time on one line?](http://stackoverflow.com/questions/9246076/how-to-print-one-character-at-a-time-on-one-line) – jophab Jan 04 '17 at 03:52

2 Answers2

5

You can do this:

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

Another option:

import sys
sys.stdout.write(z)

If your Python is running with buffered io, you might not see any output until a newline character is written. In that case, you can force writing the output immediately by adding a call to sys.stdout.flush().

wim
  • 338,267
  • 99
  • 616
  • 750
0

Try:

import time
a="mayank"
for z in a:
    time.sleep(0.1)
    print (z, end="")
kaipatel
  • 119
  • 2