Python format string Instance summary _python

Source: Internet
Author: User

The example of this article summarizes the Python format string method, shared for everyone to reference. The specific analysis is as follows:

The Python string formatting method is expressed as an example:

* Define Width
The Python code is as follows:

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

-Left alignment
The Python code is as follows:

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

A floating-point decimal with a minimum width of 6, and a space before the number of digits is insufficient
The Python code is as follows:

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

A dictionary form that shows a plus sign before a positive number, and a 0 before the digits are not enough
The Python code is as follows:

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

Display 0 (' 0 ') before the octal number, showing ' 0x ' or ' 0X ' before hexadecimal (depending on the ' 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 '

I hope this article will help you with your 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.