Bag of ML Words

ML = Machine Learning, Music Love, and Miscellaneous things in daily Livings

Python: print(f) (formated 文字列)

知りませんでした。いろいろな変数型が混在した文字列をprintするのが簡単になるっぽい。

 

Pythonのf文字列(フォーマット済み文字列リテラル)の使い方 | note.nkmk.me

 

やってみる。

 

>>> a = 123
>>> a
123
>>> b = "abc"
>>> b
'abc'
>>> c = "あいう"
>>> c
'あいう'
>>> type(c)
<class 'str'>
>>> print('{} and {} and {}'.format(a, b, c))
123 and abc and あいう
>>> print('{first} and {second} and {third}'.format(first=a, second=b, third=c))
123 and abc and あいう
>>> print(f'{a} and {b} and {c}')
123 and abc and あいう

 

なので、str()でcastしたり、 + でつないだり、それによってlintで改行が強要されたり、みたいのが減ってすっきりかけるようになる