A summary of formatted output usage in Python

Source: Internet
Author: User
This example summarizes the formatted output usage in Python. Share to everyone for your reference, as follows:

Python has a total of two formatting output syntaxes.

One is similar to the C-language printf approach, called formatting Expression

>>> '%s%d-%d '% (' hello ', 7, 1)  ' Hello 7-1 '

The other is a C #-like way, called string formatting method Calls

>>> ' {0} {1}:{2} '. Format (' Hello ', ' 1 ', ' 7 ')  ' Hello 1:7 '

The first way to specify the precision of a floating-point number, such as

>>> '%.3f '% 1.234567869  ' 1.235 '

Specify the precision of floating-point numbers dynamically at run time

But how can the code specify the precision of a floating-point number dynamically through parameters when it is running?

The magic of Python is that it provides a very handy syntax. Just add a * before TypeCode (here is f), and the precision of the floating-point number is specified using the numbers in front of it.

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

It can be seen from the output that the precision is specified dynamically at run time, thus eliminating the patchwork of formatted strings.

Using the String formatting Method Calls can be done more cleanly.

>>> for I in range (5):  ...  ' {0:. {1}f} '. Format (1/3.0, i) ...  ' 0 '  0.3 '  0.33 ' 0.333 '  0.3333 '

Implement a simple Template tool

The template language provided by Django allows us to bind the Python variable through a dict (dictionary) HTML file, in fact, using the format output of Python we can just do a text substitution function.

>>> replay = "" "... Hello World Cup ... Germany vs Brazil ...% (Germany) d:% (Brazil) d "" "  >>> print (replay% {' Germany ': 7, ' Brazil ': 1})  Hello W Orld Cup ...  Germany vs Brazil  7:1

And you can play like this.

>>> Germany = 7  >>> Brazil = 1  >>> '% (Germany) d:% (Brazil) d '% VARs ()  ' 7:1 '

Accessing object properties and dictionary key values in a formatted string

>>> ' My {1[kind]} runs {0.platform} '. Format (sys, {' kind ': ' pc '})  ' My PC runs Linux '  >>> ' my {ma P[kind]} runs {sys.platform} '. Format (Sys=sys, map={' kind ': ' pc '})  ' My pc runs Linux '

Accessing the list element in a formatted string by subscript (positive integer)

>>> 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 ']"  >>>

These are the contents of the formatted output usage summary in Python, so please pay attention to topic.alibabacloud.com (www.php.cn) for more information!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.