《Beginning Python From Novice to Professional》学习笔记五:Advanced String

1.格式化
format='hello, %s. Your ID is %d.'
values=('thy', 38)
print format % values
---> 'hello, thy. Your ID is 38.'
如第二句如示,要修饰多个元素,可以用Tuple,也可以用Dictionary(但不可以用List,因为List会被认为仅仅是一个单值

2.模板(此功能不是内置的,因此要导入string库)
import string
s=string.Template('$name is an amzaing girl $$!')
s.substitute(name='Rose')
---> 'Rose is an amzaing girl $!'   #注意最后一个$

但以上写法在下面例子中会出问题
s=string.Template('$name2nd is a great empire $$!')
s.substitute(name='Henry')

因此编译器无法区分name是单词的一部分。可改为
s=string.Template('${name}2nd is a great empire $$!')
s.substitute(name='Henry')
---> 'Henry2nd is a great empire $!'

还可以用Dictionary中的Value-Name对来替换。
s = Template('A $thing must never $action.')
d = {}   #建一个空的Dictionary
d['thing'] = 'true man'
d['action'] = 'let his girl cry'
s.substitute(d)
---> 'A good man must never let his girl cry.'

3.精度
'%10.2f' % 2 ---> '      2.00'
'%010.2f' % 2 ---> '0000002.00'   #注意在10之前的0不可更换,因为若为其它数字,会与10成为整体;若为字母,会成为转义字符
'%10.2s' % 'thy' ---> '        th'
'%.2s' % 'thy' ---> 'th'
但是'%.*s' % 'thy'会出错,这时一定要接Tuple对象,其中Tuple的第一项用来替换精度中的*,可用于由用户定制精度。
'%.*s' % (5, 'Guidovan')
---> 'Guido'
'%*.3s' % (5, 'Guidovan')
---> '  Gui'

附上一段《Beginning Python From Novice To Professional 2nd》中十分精致的示例代码:

--->
The following is a sample run of the program:
Please enter width: 35
===================================
Item                          Price
———————————————————
Apples                         0.40
Pears                          0.50
Cantaloupes                    1.92
Dried Apricots (16 oz.)        8.00
Prunes (4 lbs.)               12.00
===================================

(由于网页发布的原因,中间不对齐,事实上Item项是左对齐,Price项是右对齐)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you from “novice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值