4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3でプログラムの途中経過を改行なしで出力する

Posted at

Python3でプログラムの途中経過をで表示したい。
でも毎回改行して画面を埋め尽くしたくない。

#解決策1
改行なしで出力する場合print文のオプションにend=''を付けます。
これだけではfor文が終了した後に一気に出力されてしまうので、sys.stdout.flush()で毎回標準出力します。

import sys

for x in range(10):
    print(x, '', end='')
    sys.stdout.flush()

#解決策2
print文のオプションにflush=Trueをつけることで毎回標準出力します。

for x in range(10):
    print(x, '', end='', flush=True)
4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?