3.1Python字符串格式化

学习目标:

①:Python字符串格式化

  • Python日常

内容展示:

①:Python字符串格式化

# 第一题:格式化字符串的方式
"""
1. 模板字符串
2.%格式化
3.字符串的format方法
4.fstring
"""

# 通过template对象封装  $放置一些占位符,并通过substitute方法用实际的值替换这些占位符

from string import Template

template1 = Template('$s是我最喜欢胡编程语言, $s非常容易学习,而且功能强大')
print(template1.substitute(s='Python'))
# Python是我最喜欢胡编程语言, Python非常容易学习,而且功能强大

print(template1.substitute(s='PHP'))
# PHP是我最喜欢胡编程语言, PHP非常容易学习,而且功能强大

template2 = Template('${h}ello world')
# print(template2.substitute(hello='abc'))
print(template2.substitute(h='abc'))
# abcello world


template3 = Template('$dollar$$相当于$pounds英镑')
print(template3.substitute(dollar=20, pounds=16))
# 20$相当于16英镑


data = {}
data['dollar'] = 30
data['pounds'] = 25
print(template3.substitute(data))
# 30$相当于25英镑



总结:

  1.
  模板字符串是Template类封装的一个字符串,可以用 $指定占位符,并使用subtitute方法替换掉这些占位符


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿中在线啃码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值