fstringを使うと、文字列フォーマットが直感的で簡潔に済みます。
Python3.6からの機能なので、Python3でもそれ以前のバージョンやPython2の方はお気をつけください。
>>> apple = 100 >>> orange = 200 >>> total = apple + orange >>> print(f'合計:{total}円です') # f''を使う、中の変数は{}で囲む 合計:300円です
もしフォーマット関数を使ったなら、
>>> apple = 100 >>> orange = 200 >>> total = apple + orange >>> print('合計:{}円です'.format(tatal)) 合計:300円です
>>> py = "PythonCarnival" >>> f'これからも{py}をよろしくお願いします!' 'これからもPythonCarnivalをよろしくお願いします!' >>> x = [1, 2, 3, 4, 5] >>> f'{x}' '[1, 2, 3, 4, 5]' # リストの中身を表した文字列になった >>> def foo(x:int) -> int: # x:intや-> intはアノテーションです。 ... return x + 1 ... >>> f'{foo(0)}' # {}の中身は関数でもOK '1'