Summary of formatted output usage in python

黄舟
Release: 2016-12-28 17:59:40
Original
1458 people have browsed it

The examples in this article summarize the usage of formatted output in python. Share it with everyone for your reference, the details are as follows:

Python has a total of two formatted output syntaxes.

One is similar to C language printf, called Formatting Expression

>>> '%s %d-%d' % ('hello', 7, 1)  
'hello 7-1'
Copy after login

The other is similar to C#, called String Formatting Method Calls

>>> '{0} {1}:{2}'.format('hello', '1', '7')  
'hello 1:7'
Copy after login

The first way can specify the precision of floating point numbers, for example

>>> '%.3f' % 1.234567869  
'1.235'
Copy after login

Dynamically specify the precision of floating point numbers during runtime

But how to dynamically specify floating point numbers through parameters when the code is running? What about accuracy?

The magic of python is that it provides a very convenient syntax. Just add a * before the typecode (here f), and the precision of the floating point number is specified by the number in front of it.

>>> for i in range(5):  
... '%.*f' % (i, 1.234234234234234)  
...  
'1'  
'1.2'  
'1.23'  
'1.234'  
'1.2342'
Copy after login

It can be seen from the output results that the precision is dynamically specified at runtime, which eliminates the need to piece together format strings.

Using String Formatting Method Calls can complete the function more concisely.

>>> for i in range(5):  
...  '{0:.{1}f}'.format(1 / 3.0, i)  
...  
'0'  
'0.3'  
'0.33'  
'0.333'  
'0.3333'
Copy after login

Implementing a simple template tool

The template language provided by Django allows us to bind python variables to the html file through a dict (dictionary). In fact, it uses python formatting For output we can also just do a text replacement function.

>>> replay = """ 
... Hello World Cup... 
... Germany vs Brazil 
... %(germany)d : %(brazil)d"""  
>>> print(replay % {'germany': 7, 'brazil': 1})  
Hello World Cup...  
Germany vs Brazil  
7 : 1
Copy after login

You can also play like this

>>> germany = 7  
>>> brazil = 1  
>>> '%(germany)d : %(brazil)d' % vars()  
'7 : 1'
Copy after login

Access object properties and dictionary key values ​​in the format string

>>> 'My {1[kind]} runs {0.platform}'.format(sys, {'kind': 'pc'})  
'My pc runs linux'  
>>> 'My {map[kind]} runs {sys.platform}'.format(sys=sys, map={'kind': 'pc'})  
'My pc runs linux'
Copy after login

In the format string through subscripts (positive integers )Access list elements

>>> somelist = list('SPAM')  
>>> 'first={0[0]}, third={0[2]}'.format(somelist)  
'first=S, third=A'  
>>> 'first={0}, last={1}'.format(somelist[1], somelist[-1])  
'first=P, last=M'  
>>> parts = somelist[0], somelist[-1], somelist[1:-1]  
>>> 'first={0}, last={1}, middle={2}'.format(*parts)  
"first=S, last=M, middle=['P', 'A']"  
>>>
Copy after login

The above is a summary of formatted output usage in python. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!