Print in one line dynamically

后端 未结 20 3218
梦谈多话
梦谈多话 2020-11-21 23:32

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

Specifically, suppose I have:

for it         


        
20条回答
  •  执念已碎
    2020-11-21 23:52

    You can add a trailing comma to your print statement to print a space instead of a newline in each iteration:

    print item,
    

    Alternatively, if you're using Python 2.6 or later, you can use the new print function, which would allow you to specify that not even a space should come at the end of each item being printed (or allow you to specify whatever end you want):

    from __future__ import print_function
    ...
    print(item, end="")
    

    Finally, you can write directly to standard output by importing it from the sys module, which returns a file-like object:

    from sys import stdout
    ...
    stdout.write( str(item) )
    

提交回复
热议问题