Beginning Python from Novice to Professional (4)-Demo sample format string

Source: Internet
Author: User
Tags print format

$ gedit price.py

#!/usr/bin/env pythonwidth = input (' Please enter width: ') price_width = 10item_width = Width-price_widthheader_format = '%-*s%*s ' format        = '%-*s%*.2f ' print ' = ' * widthprint 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, ' apricots ', price_width,8) print format % (Item_width, ' prunes ', price_width,12) print ' = ' * width
$ python price.py
Please enter Width:35===================================item price                          -----------------------------------Apples                         0.40Pears                          0.50Cantaloupes                    1.92Apricots                       8.00Prunes                        12.00================================== =
Minus sign (-) to align the values to the left

Asterisk (*) resolution reads parameters from the expression tuple

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Beginning Python from Novice to Professional (4)-Demo sample format string

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.