1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

print() は、Python組み込みの関数で、文字列や値を出力するために使用します。最も基本的な使い方は以下の通りです。

print('Hello, World!')

この例では、シンプルに 'Hello, World!' という文字列が出力されます。

print() 関数には、いくつかの引数を渡すことができます。

  • *objects - 出力したいオブジェクト(値や変数)を指定します。複数の値はカンマ , で区切ります
x = 5
y = 10
print(x, y)  # 出力: 5 10
print('X =', x, ', Y =', y)  # 出力: X = 5 , Y = 10
  • sep=' ' - 出力する値の間の区切り文字列を指定します。デフォルトは空白文字です
print('A', 'B', 'C', sep='-')  # 出力: A-B-C
  • end='\n' - 出力の最後に付加する文字列を指定します。デフォルトは改行文字 \n です
print('Hello', end='')
print('World')  # 出力: HelloWorld
  • file - 出力先のファイルオブジェクトを指定できます。デフォルトは sys.stdout です
  • flush=False - 出力をバッファリングするかどうかを指定します。True を指定すると、すぐに出力が行われます

また、フォーマット指定子を使って、文字列のフォーマットを制御することもできます。

x = 10
y = 5.25
print('X = %d, Y = %.2f' % (x, y))  # 出力: X = 10, Y = 5.25

Python 3.6以降では、フォーマット済み文字列リテラル(f-strings)が導入されました。

name = 'Alice'
age = 25
print(f'My name is {name} and I am {age} years old.') # 出力: My name is Alice and I am 25 years old.

print() は非常に柔軟で便利な関数です。デバッグ時の値の確認、ユーザーへの情報出力、ログ出力など、さまざまな用途に使用されます。

参考) 東京工業大学情報理工学院 Python早見表

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?