Formatted Strings

In Python language, formatting of strings and inserting of variables in a string can be done in following ways -

Option 1: %-format

>>> name = "Jugal"
>>> "Hey there %s!" % name
'Hey there Jugal!'

Option 2: str.format()

>>> name = "Jugal"
>>> "Hey there {0}!".format(name)
'Hey there Jugal!'

Option 3: f-strings

>>> name = "Jugal"
>>> f"Hey there {name}!"
'Hey there Jugal!'

Source: RealPython

Last updated