Python String Formatting Example

Source: Internet
Author: User

Look at the code first.

#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = ' jiang ' __creattime__ = ' 2015/10/31 23:40 ' width = input (' Please ENT Er width: ') price_width = 10item_width = Width-price_widthheader_format = '%-*s%*s ' format = '%-*s%*.2f ' print ' = ' * WIDTHP Rint Header_format% (item_width, ' item ', Price_width, ' price ') print '-' * widthprint format% (item_width, ' Apples ', Price_ width,0.4) print format% (item_width, ' Pears ', price_width,0.5) print format% (item_width, ' cantaloupes ', Price_width, 1.92) print format% (item_width, ' dried apricots (+ oz.) ', price_width,8) print format% (Item_width, ' prunes (4 lbs) ', price _width,12) print ' = ' * width

OK, let's see what the print looks like.

==================================================item                                         Price--------------------------------------------------Apples                                        0.40Pears                                         0.50Cantaloupes                                   1.92Dried Apricots (oz.)                        8.00Prunes (4 lbs)                                12.00==================================================

It's OK, but there's a problem, try to be as big as you can when you assign a value to width. If you assign a value that is too small, such as 20, the price on the right will not be aligned. Interested can run one side of the code.

Say a few key points. Head_format = '%-*s%*s ', string format operator% followed by S means that any Python object can be formatted with STR. '-' means left-justified. ' * ' indicates that the field width and precision can be received.

Here is the string format conversion type, perhaps you can look at it.

  

Conversion type                                        meaning D,i                                    signed decimal integer o unsigned                                      octal u unsigned                                      decimal x unsigned                                      hexadecimal (lowercase) x                                      unsigned hexadecimal (uppercase) e                                      . Scientific notation represents floating-point (lowercase) E-                                      scientific notation for floating-point numbers (uppercase) f,f                                    decimal Floating-point number R                                      strings (using repr to convert any Python object) s                                      string (convert any Python object using str)

For more string conversion types you can search the Internet for a bit.

Simple conversions:

    

>>> ' Price of eggs: $%d '% "price of eggs: $42 ' >>>from math import pi>>> ' pi:%f ... '% pipi : 3.141593...>>> ' My age was%s '% 42L ' my age is ">>> ' my age was%r '% 42L ' my age is 42L '

The difference between STR and repr, you can go to search, I do not repeat here.

Click here to communicate with me

Python String Formatting Example

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.