4
8

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.

[Python小ネタ]同一カーソル位置で文字を連続でprintする

Last updated at Posted at 2018-07-17

printする文字の前に\033[Aをつけます。
そのままfor文でprintしてしまうと早すぎてわからないので、今回はtime.sleepを使ってスピードを調整しています。
また、\033[3{i}mを使うことで、出力する文字に色もつけて遊べます。

a.py
import time

print("連続で出力するよ\n")
for i in range(8):
    print(f"\033[A{i}")
    time.sleep(0.5)

print("色もつけてみたよ\n")
for i in range(8):
    print(f"\033[3{i}m\033[A{i}")
    time.sleep(0.5)

これを実行するとこんな感じで出力されます。

a.gif

4
8
0

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
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?