Home  >  Article  >  Backend Development  >  How to use format in python

How to use format in python

coldplay.xixi
coldplay.xixiOriginal
2021-03-09 15:43:32195584browse

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')].

How to use format in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn