You have two formatting options:
- Old
%python2 style statement that works with C-printf-style format strings - New
format()python3 style method for string. Due to backport, you can use this option in both python2 and python3. - Even newer Python 3. 6+ lines that are not wrapped in python2: they allow you to specify expressions inside string literals
% C-printf-style:
"Hello %s" % subs
"Hello %s%s" % (subs, '.')
my_dict = {'placeholder_name': 'world', 'period': '.'}
"Hello %(placeholder_name)s%(period)s" % my_dict
, python %s ( , ..) str() . %r repr() . , %f, , (, ) (, %.5f).
format():
"Hello {}".format(subs)
"Hello {placeholder_name}".format(placeholder_name=subs)
, placeholder_name subs, , . , :
"Hello {subs}".format(subs=subs)
python , , :
my_tuple = ('world', '.')
"Hello {}{}".format(*my_tuple)
my_dict = {'subs': 'world', 'period': '.'}
"Hello {subs}{period}".format(**my_dict)
, , format().
, ( args of format()) , , . , .
3. 6+ f-:
>>> x=5
>>> y=6
>>> f'woof {x*y} meow'
'woof 30 meow'