Pythonのprint文の使い方は?
print文はPythonで情報を出力するための方法の一つです。通常、以下の構文が使用されます。
print("要输出的内容")
Here you can use “output content” as a string, a variable, or even a combination of multiple variables. Here are some examples using the print statement: Example 1: Print a string.
print("Hello World")
変数の値を出力します。
name = "Alice"
print(name)
複数の変数の値を出力する時は、コンマで区切ってください。
age = 18
print(“My name is”, name, “and I am”, age, “years old”)
複数の変数の値を出力する際には、フォーマット文字列を使用してください。
print("My name is %s and I am %d years old" % (name, age))
例4では、%sと%dはフォーマット文字列のプレースホルダーであり、それぞれ文字列と整数型の変数を表します。%の後には、プレースホルダーに埋め込む変数の値が順番に括弧内に指定されています。Python 3.xバージョンでは、printは文ではなく関数です。したがって、print文を使う際には、括弧内に出力する内容を入れる必要があります。例えば:
print("Hello World")