$ gedit price.py

#!/usr/bin/env python
width = input('Please enter width: ')
price_width = 10
item_width = width - price_width
header_format = '%-*s%*s'
format = '%-*s%*.2f'
print '=' * width
print header_format % (item_width, 'Item', price_width, 'Price')
print '-' * width
print 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.40
Pears 0.50
Cantaloupes 1.92
Apricots 8.00
Prunes 12.00
===================================

减号(-)用来左对齐数值

星号(*)分辨率从表达元组读取参数

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Beginning Python From Novice to Professional (4) - 演示样本格式字符串的更多相关文章

  1. Beginning Python From Novice to Professional (7) - 类别

    类别 创建一个简单的类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.n ...

  2. Beginning Python From Novice to Professional (9) - Socket

    Socket 小型server: #!/usr/bin/env python import socket s = socket.socket() host = socket.gethostname() ...

  3. Beginning Python From Novice to Professional (5) - 条件与循环

    条件与循环 条件运行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumb ...

  4. Beginning Python Chapter 1 Notes

    James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...

  5. Python从菜鸟到高手(7):字符串

    1. 单引号字符串和转义符   字符串与数字一样,都是值,可以直接使用,在Python控制台中直接输入字符串,如"Hello World",会按原样输出该字符串,只不过用单引号括了 ...

  6. 模式识别 - 处理多个演示样本研究(MIL)特点(matlab)

    处理多个演示样本研究(MIL)特点(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习 ...

  7. Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)

    Cocos2d-x 3.2 Lua演示样本CocosDenshionTest(音频测试)  本篇博客介绍Cocos2d-x 3.2中Lua演示样例的音频測试.Cocos2d-x使用SimpleAudi ...

  8. java注意事项演示 地图产生表 演示样本 来自thinking in java 4 20代码的章

    java注意事项演示 地图产生表 演示样本  来自thinking in java 4 20代码的章 thinking in java 4免费下载:http://download.csdn.net/d ...

  9. Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式

    Python第三天 序列  5种数据类型  数值  字符串  列表  元组  字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天  安装  shell ...

随机推荐

  1. JSP的学习(6)——九大隐式对象及其out对象

    本篇将介绍JSP中的九大隐式对象,并重点介绍其中的out对象. 我们在之前的博客<JSP的学习(1)——基础知识与底层原理>一文中已经知道,JSP最终要被翻译和转换成Servlet,在转换 ...

  2. <IMG>中UserMap的用法

    usemap是<img>标签的一个属性,用作指明所使用的图像地图名. 后面的#Map的Map就是用<map>标签定义的一个图像地图,它的name属性是Map,像下面这样定义: ...

  3. XML wsdl soap xslt xsl ide

    XML wsdl soap  xslt xsl ide 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn. ...

  4. 中介者模式 C++ 实现

    #include<iostream> #include<string> #include<vector> #include<cstdlib> using ...

  5. Eclipse shift + ctrl + F 不好用

    出现 shift + Ctrl + F  整理代码没有反应的情况,先检查下输入法是否是英文的,切换英文后再尝试.

  6. ABP启动配置

    ABP启动配置   返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...

  7. Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度 1.QObject类的定时器 QObje ...

  8. Lucene.Net 2.3.1开发介绍——附录一、如何下载Lucene.Net的各种版本

    原文:Lucene.Net 2.3.1开发介绍--附录一.如何下载Lucene.Net的各种版本 首先,你需要一个svn客户端.TortoiseSVN非常好用,可以从官方网站下载.下载地址:http: ...

  9. Swift - UIColor使用自定义的RGB配色

    1,比如rgb 色值为55. 186 .89 那么给UIColor设置里面要除以255 1 UIColor(red: 55/255, green: 186/255, blue: 89/255, alp ...

  10. nginx tcp proxy 连接保持设置

    根据前文Nginx tcp proxy module试用的设置,在测试环境中发现tcp连接经常掉线.在该项目站点上找到一个issue,也谈论这件事情,不过别人用在web socket协议上. 其实就是 ...