str = "Hello,%s.%s enough for ya ?"
values = ('world','hot')
print str % values
Hello,world.hot enough for ya ?
#coding=utf-8
from string import Template
## 单个变量替换
s1 = Template('$x, glorious $x!')
print s1.substitute(x = 'slurm')
## 美元符号表示以及单个变量的替换
s2 = Template("Make $$ selling $x!")
print s2.substitute(x = 'slurm')
## 字段变量的替换
s3 = Template('A $thing must never $action .')
d = {}
d['thing'] = 'gentleman'
d['action'] = 'show his socks'
print s3.substitute(d)
ps:safe_substitute 不会因缺少值或者不正确使用$字符而出错。
print 'With a moo-moo here, and a moo-moo there'.find('moo')
返回:7
print '/'.join((' ','usr','bin','env'))
输出: /usr/bin/env
ps:和谷歌的guava有点像。
print 'AK47'.lower() 输出:ak47
'This is a test'.replace('is','ezz') 输出:Thezz ezz a test
print '1+2+3+4+5'.split('+')
输出:['1', '2', '3', '4', '5']
ps:另外可以参加lstrip和rstrip方法。
ps:maketrans方法和translate方法类似。
dict(name='Gumby',age=42)
dict([('name','Gumby'),('age',42)])
ps:都是构造字段的方法。
print {}.fromkeys(['name','age'])
输出:{'age': None, 'name': None}
d = {}
d['name'] 如此访问时会报错。
d.get('name')访问时,如果不存在会返回None。
items方法会将字典按照键值元组列表的形式返回,但没有顺序。 iteritems和items类似,但是返回的是迭代器。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有