Learn Python by Building Data Science Applications
上QQ阅读APP看书,第一时间看更新

F-strings

F-strings are relatively new features of Python. They were released in version 3.6 and are both elegant and faster to execute because everything is computed on the go. Here is one example (note the letter f before the starting single quote symbol):

>>> adj = ‘beautiful'
>>> f'Hello {adj} world!'
Hello beautiful world

Everything inside the curly brackets is actually executable code. Inside this brackets, you can use arithmetic or even run functions:

>>> N = 99
>>> f'{N-1} bottles of milk on the wall.'
'98 bottles of milk on the wall.'

>>> name = '
pHILIPP'
>>> f'Hello mr{name.title()}'
'Hello mr. Phillipp'