0
1

More than 3 years have passed since last update.

【Python】print関数

Last updated at Posted at 2020-01-06

Print関数でできる事

メッセージや変数の中身を表示することができる。

メッセージを表示する

メッセージを表示したい場合は、ダブルクォーテーションで表示内容を囲むこと!

print.py
print("ここに表示したいメッセージを記入する")
# output = ここに表示したいメッセージを記入する

変数の中身を表示する

変数の中身を表示したい場合は、print()内にそのまま記入する。
変数に文字を格納する場合は、ダブルクォーテーションが必要
変数に数値を格納する場合は、ダブルクォーテーションは不要

print.py
text="あいうえお"
print(text)
# output = あいうえお

メッセージ+変数の中身の表示する方法

いくつかの方法がありますが、ここではその一例を示します。
使う関数はformat()です。
format()文章内の{x}にformat()内のx番目の変数を対応させる関数です。

print.py
pi=3.14
r=5
print("円周率は{0}で、半径は{1}です。半径は{1}で円周率は{0}です。".format(pi,r))
# output = 円周率は3.14で、半径は5です。半径は5で円周率は3.14です。
0
1
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
0
1