python - How to print without newline or space? -


The question is in the title.

What I want to do in this example:

  #include & lt; Stdio.h & gt; Int main () {int i; For (i = 0; i & lt; 10; i ++) printf ("."); Return 0; }  

Output:

  ..........  

In Python:

  & gt; & Gt; & Gt; In the xrange (0,10): print '.' . . . . . . . . . . & Gt; & Gt; & Gt; For I am in xrange (010): print '.',. . . . . . . . . .  

In Python, print will add a \ n or a location, how can I avoid it? Now, this is just an example. Tell me, I can first construct a string and print it. I want to know how to add "code" to stdout .

normal way

  import system sys.stdout.write (' . ')  

You may also need to call

 < stdout  to ensure the code> sys.stdout.flush ()  

is immediately flushed. Python 2.6 from Python 2.6 You can import the print function from Python 3: <__ future__ import from print_function

   

This allows you to use the Python 3 solution below.

Python 3

In Python 3, the print statement has been changed into function. In Python 3, you can do this instead:

  print ('.', End = '')  

Also works in Python 2 , Provided that you have used the __future__ import print_function .

If you are having trouble buffering, then you can flush the output by adding the keyword log:

  '.', End = '', flush = true)  

Comments