Python Print的扩展


1.将Format后面的内容以此填充
print "I am {0}, and I like {1}".format('Sharon', 'Python')
 
2.用关键词来指明所应该填充的内容
 
 
print "I am {name}, I like {all}, this is my tel {0}".format(13511111,name='Sharon', all=['Python','Java','C#','C'])
返回:I am Sharon, I like ['Python', 'Java', 'C#', 'C'], this is my tel 13511111
3.序列对象的偏移量,很有用
  
  
print "First char of word {0} is {0[0]}, Second char is {0[2]}".format('Python')
返回:First char of word Python is P, Second char is t
Note: 值得注意的是,在模板中不能使用负数偏移,但是在外面可以
print "First char of word {0} is {0[-1]}, Second char is {0[2]}".format('Python')
   print "First char of word {0} is {0[-1]}, Second char is {0[2]}".format('Python')
TypeError: string indices must be integers, not str
放在外面:
	word = 'Hello'
	print "First char is {0}, second char is {1}".format(word[0],word[1])
4. 除了根据位置得到,还能根据Dictionary的key得到,
infoDic={'name':'Sharon', 'qq':18209548, 'tel':13511111}
template='My name is {myinfoDic[name]}, my QQ is {myinfoDic[qq]}'
print template.format(myinfoDic=infoDic)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值