1
0

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.

数値のアウトプットをキレイに見せる

1
Last updated at Posted at 2018-03-07

初歩的ですが最後の「3列で整地する」を知らなかったのでメモとして。

改行なし、/をつける、3列にする

qiita.py
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # range(10)

# まずは改行なし。
print ('pattern 1 (no end):')
for i in num:
    print (str(i))

# 次に、endに'/'をつける。
print ('pattern 2 (with end):')
for i in num:
    print (str(i), end=' / ') # end を指定すると出力した後に改行されない
print() # 改行を入れる
# 連続したアウトプットのときはprint()で改行できます。

# 3列でキレイに整える。
print ('pattern 3 (3 columns):')
for i in num:
    print (str(i), end='    ')
    if (i + 1) % 3 ==0: # 3 つ処理するごとに改行を入れる
        print()

アウトプット

見づらいのですが、こんな感じになります。

pattern 1 (no end):
0
1
2
3
4
5
6
7
8
9
pattern 2 (with end):
0 / 1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9 /
pattern 3 (3 columns):
0 1 2
3 4 5
6 7 8
9

3列バージョンは便利なので覚えておこう。

1
0
6

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?