プログラミングで文字列の操作は切っても切り離せない。文字列の結合をする際に、Pythonではどのような方法があるのだろうか。どの方法を用いると見通しの良いコードになるだろうか。
開発環境:
Python 3.9.2
例1:+演算子を用いた結合
>>> a,b = 'hello','world'
>>> c = a + ' ' + b + '!'
>>> print(c)
hello world!
例2:formatを用いた結合
>>> a,b = 'hello','world'
>>> c = '{} {}!'.format(a,b)
>>> print(c)
hello world!
例3:f文字列(f-strings)を用いた結合(Python3.6〜)
>>> a,b = 'hello','world'
>>> c = f'{a} {b}!'
>>> print(c)
hello world!
まとめ
より良いアプローチについて考え、より美しくしよう
知識は陳腐化しがちであるため、時々「What's New in Python」に目を通そう
やっつけ仕事に対する悔いは成長を促す