Pythonのprint文法

Pythonでは、print文を使用することでコンソールやファイルに出力を出すことができます。以下にprint文を使用した例を示します。

  1. 文字列を出力する:
print("Hello, World!")
  1. 出力変数の値を出力する:
name = "Alice"
age = 25
print(name)
print(age)
  1. 複数の変数の値を返します:
x = 10
y = 20
print("x =", x, "and y =", y)
  1. フォーマット通りの出力です
name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
  1. エスケープ文字を使用:
print("This is a new line.\nThis is a tab.\tThis is a backslash: \\")

Python2.xでは、printは関数がなくステートメントなので、直接使用できます。 Python3.xでは、printは組み込み関数で、出力する内容を括弧で囲む必要があります (例: print(“Hello, World!”) )。

bannerAds