and C The language is the same , Followed by several variables or values , Only one time bracket can be omitted
>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 10000)
'Hi, runner, you have $10000.'
Place holder
replace content
%3d %-3d %03d %+3d
Integers ( You can specify a complement 0 Or space (‘-’ Means to fill in a space on the right ), Or make up one +)
%2.2f %-2.2f …
Floating point numbers ( ditto , And the number of decimal places can be specified )
%s
character string ( It works forever , It converts any data type to a string )
%x
Hexadecimal integer
>>> print('%4d-%03d' % (3, 1))
3-001
>>> print('%-4d-%03d' % (3, 1))
3 -001
>>> print('%+4d-%03d' % (3, 1))
+3-001
>>> print('%10.2f' % 3.1415926)
3.14
>>> print('%d%%' % 3)
3%
use %%
To represent a %
character
>>> 'Hello, {0}, The results have improved {1:.1f}%'.format(' Xiao Ming ', 22.125)
'Hello, Xiao Ming , The results have improved 22.1%'