python第八天-下雨天

python模板字符串和格式化字符串
模板字符串:用string模块里的Template

Template()里面把字符串中某个值用设置变量${key}的方式先写好,然后在substitute()的方式把变量用其他值代替,就完成了字符串的替换

from string import Template

a=Template(‘would it be the ${key1} when we meet in ${key2}’)

a.substitute(key1=‘same’,key2=‘heaven’)
‘would it be the same when we meet in heaven’

格式化字符串:用%s

在字符串中某个需要后期替换的字符,先用%s代替,然后在字符串外边再用%()把字符串中的%换成目标字符串,就实现了字符串格式化

“would it be the %s when we meet in %s”%(‘same’,‘heaven’)
‘would it be the same when we meet in heaven’

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值