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

More than 1 year has passed since last update.

【Python 備忘1】print 関数

Posted at

Print 関数

ここでは、文字列や数値などのオブジェクトを出力するための print関数 について記載していきます。
単に print といっても、さまざまな出力方法があります。
基本的な使い方は普段使いますが、用途が限られた使い方もたくさんあるので、
用途にあった出力方法をメモしておきます。

目次

  • 通常出力
    • 数値出力
    • 文字列出力
    • 変数出力
    • 複数出力
  • オプション

通常出力

数値出力

数値を出力する場合は、そのまま記載します。

test.py
print(10)
実行結果
10

文字列出力

文字列を出力する場合は、ダブルクォーテーション「 " 」 や シングルクォーテーション「 ' 」 で、
囲んで記載します。

test.py
print("Hello World")
実行結果
Hello World

変数出力

数値や文字列、リストや辞書が格納された変数を記載することで、
それらを表示することも可能です。

test.py
number = 10
strings = "Hello World"
lists = [10,"Hello World"]
dicts = {'one': 1, 'two': 2, 'three': 3}
print(number)
print(strings)
print(lists)
print(dicts)
実行結果
10
Hello World
[10, 'Hello World']
{'one': 1, 'two': 2, 'three': 3}

複数出力

文字列と数値を共に出力したい場合は、数値を str() 関数で文字列に変換する必要があります。

test.py
age=10
print("私は" + str(age) + "歳です.")
実行結果
私は10歳です.

str()関数を使用しない場合は下記エラーが出力されます。

test.py
age=10
print("私は" + age + "歳です.")
実行結果
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print("私は" + age + "歳です.")
          ~~~~~^~~~~
TypeError: can only concatenate str (not "int") to str

オプション

print 関数にはいくつかのオプションがあります。

sep:区切り文字の指定(デフォルト:半角スペース)

print 関数で複数のオブジェクトを同時に print した場合、それぞれ半角スペースで区切られます。
この区切り文字を変更する際に使うのが、sep オプションです。

test.py
print("Hello","World")
print("Hello","World",sep=",")
print("Hello","World",sep="")
print("Hello","World",sep="\n")
実行結果
Hello World
Hello,World
HelloWorld
Hello
World

end:末尾の文字の指定(デフォルト:改行)

print 関数でオブジェクトを出力すると、末尾に改行が出力されます。
出力内容の末尾を変更する際に使うのが、end オプションです。

test.py
print("Hello")
print("World")
print("Hello",end="")
print("World")
print("Hello",end=" ")
print("World")
実行結果
Hello
World
HelloWorld
Hello World

file:出力先の指定(デフォルト:標準出力)

print 関数でオブジェクトを出力すると、
pythonを実行したコンソール上に内容が出力されます。
出力先を指定することができるのが、file オプションです。

test.py
with open('test.txt','w') as f:
    print("Hello World at console")
    print("Hello World at text file",file=f)
    print("Hello World at text file",file=f)
実行結果
Hello World at console
test.txt
Hello World at text file
Hello World at text file

flush:バッファリングなしで出力するかどうかを指定:(デフォルト:False)

処理に時間がかかってしまうような状況で__print__ 関数でオブジェクトを出力する際、
なかなか出力されないことがあります。
そんな時にバッファリングなしで出力することができるのが、flush オプションです。
※出力する内容は特に変わりません。

test.py
print("Hello World")
print("Hello World",flush=True)
実行結果
Hello World
Hello World

あとがき

ここまで見てくださり、ありがとうございました。
普段pythonを使ったり使わなかったりしていますが、
ふと使った際に、これどうやるんだっけ、とか スペルどうだったっけ(英語苦手です…)と
なってしまうことが多々あったので、備忘としてメモのように残していこうと思います。
このような記事を書くのは初めてのことなので、これから少しずつ慣れていけたらなと思っています。

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