LoginSignup
41
43

More than 5 years have passed since last update.

【Python】printを使う際に注意すること

Last updated at Posted at 2016-09-02

Python3でprintを使う際は、sepとendのオプションは頭の片隅に意識しておいた方が良い。
非常に基本的な事だが、忘れてしまうと意図しない結果を出力してしまうことがある。

s = 'abc'
t = 'def'

# カンマやタブの前後に空白が入る
print(s, ',', t) # -> abc , def
print(s, '¥t', t) # -> abc ¥t def

# カンマやタブの前後に空白を入れたくない場合はsepに空の文字列を指定
print(s, ',', t, sep='') # -> abc,def
print(s, '¥t', t, sep='') # -> abc¥tdef

# 改行したくない場合はendに空の文字列を指定
print(s, ',', t, sep='', end='') # -> abc,def(改行なし)
print(s, '¥t', t, sep='', end='') # -> abc¥tdef(改行なし)
41
43
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
41
43