Python formatted string instance summary

Source: Internet
Author: User
This article summarizes the Python format string method, share to everyone for your reference. The specific analysis is as follows:

The Python string format method is described in the form of an example:

* Define Width
The Python code is as follows:

>>> '%*s '% (5, ' some ') ' some '

-Align Left
The Python code is as follows:

>>> '%-*s '% (5, ' some ') ' some '

Floating-point decimals with a minimum width of 6 and a 2-bit precision, with insufficient digits to fill the space before
The Python code is as follows:

>>> '%6.2f '%8.123 ' 8.12 '

Dictionary form to display a plus sign before a positive number, 0 when the digits are not enough
The Python code is as follows:

>>> '% (name) s =% (num) +06.2f '%{' name ': ' A ', ' num ': 8.123} ' a = +08.12 '

Displays 0 (' 0 ') before the octal number, preceded by ' 0x ' or ' 0X ' in hexadecimal (depending on ' x ' or ' x ')
The Python code is as follows:

>>> ' Dec:%d/oct:% #o/hex:% #X '% (123,123,123) ' dec:123/oct:0173/hex:0x7b '

Scientific counting method
The Python code is as follows:

>>> '%e '% 1234.567890 ' 1.234568e+03 '

Hopefully this article will help you with Python programming.

  • 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.