2
3

More than 3 years have passed since last update.

改めてprint() print()のキーワード引数

Last updated at Posted at 2020-01-02

こんにちは。初めて記事を書きます。
1番使っている言語はPythonで、C言語とその他もろもろを少しかじっています。
print()はPythonの入門によく使われますが、キーワード引数が使えることを最近知り、備忘録がてらまとめてみました。

1月6日追記:バージョンは3.5.3でした。(全然違った)また、例のword = Pythonのところに''(シングルクォーテーション)を付け忘れていたので修正しました。すみません。


2021/4/18 追記 上書き出力の説明と必要なくなった部分の削除とコード例の修正。

Pythonで上書き出力。

書式) print("\r 文字列") or pirnt("\r" + "文字列")

print("\rhello")
input() #処理を一時停止Enterで再開。
print("word!")

#使用例

import time
for i in range(100):
    print("\r" + str(i) + "%")
    time.sleep(0.1) 

#同じ行に%表示が上書き出力される。


ドキュメントから引用

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
objects を sep で区切りながらテキストストリーム file に表示し、最後に end を表示します。sep 、 end 、 file 、 flush を与える場合、キーワード引数として与える必要があります。


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

test = 'てすと'
print(test)
#てすと

#キーワードで区切る
word = 'Python'
print(test, word, sep='と')
#てすととPython

#最後の行に出力する文字列を指定
print(test, end='と')
#てすとと

#応用?
print(test, end='')
#改行しない

改行しない出力で、表現の幅が広がると思うので、ぜひ参考にしてください。

2
3
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
2
3