How to use format in python: [format():] Replace the traditional [%] with [{}] to achieve formatted output. The code is ['numbers {1}{2} and { 0}'.format("123",456,'789')].
The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer.
How to use format in python:
format() formatted output
format(): replace the traditional % with {} Realize formatted output
Common usage of format():
is actually the content after format(), filled in the curly brackets (you can press the position, or press Variables)
'数字{1}{2}和{0}'.format("123",456,'789') >>>'数字456789和123'
#这里注意有两层大括号,输出的结果只有一层大括号 '数字{{{1}{2}}}和{0}'.format("123",456,'789') >>>'数字{456789}和123'
#允许一个参数用两次 '{1}{0}{1}岁'.format('jc',22) >>>'22jc22岁'
#可以通过添加关键字参数 '{name}{age}岁'.format(age=22,name='jc') >>>'jc22岁'
Related free learning recommendations:python video tutorial
The above is the detailed content of How to use format in python. For more information, please follow other related articles on the PHP Chinese website!