LoginSignup
0
3

More than 5 years have passed since last update.

[Python]可変長引数を用いたリスト要素の表示

Posted at

Pythonでリスト要素を全て表示する方法はいくつもあるが、加工等をせず単に表示をするだけなら、可変長引数を用いる方法が最も簡単。

以下のソースコードはPython 3での実行を想定

リスト要素を全て表示

ソースコード例

words = ["新", "米", "大統領"]
print(*words)

出力結果

新 米 大統領

デフォルトだとスペース区切りで出力され、最後に改行が行われる。

カンマ区切りで表示

printにsepを指定することで、任意の文字で区切ることができる。区切り文字が要らないのであれば空文字列を指定する。

ソースコード例

words = ["新", "米", "大統領"]
print(*words, sep=",")

出力結果

新,米,大統領

区切らずに表示

ソースコード例

words = ["新", "米", "大統領"]
print(*words, sep="")

出力結果

新米大統領

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