Python字符串基础(二)


一、模板字符串

在string模块中提供了一个用于格式化字符串的Template类,该类的功能是用同一个值替换所有相同的格式化参数。Template类的格式化参数用美元($)开头,后面跟着格式化名称,相当于变量名。在格式化时,需要使用Template类的substitute方法,该方法用于指定格式化参数对应的值。

from string import Template
template=Template('$s $s $s')
template.substitute(s='hello')
#这种参数被称为关键字参数
#输出结果:'hello hello hello'

在上面的代码中,通过Template类的构造方法传入一个格式化字符串,在这个格式化字符串中包含了三个“ s ” , 然 后 调 用 s u b s t i t u t e 方 法 格 式 化 这 个 字 符 串 , 该 方 法 中 指 定 了 s 参 数 值 为 ‘ H e l l o ’ , 最 后 替 换 结 果 是 ′ h e l l o h e l l o h e l l o ′ , 也 就 是 说 , 在 格 式 化 字 符 串 中 , 有 多 少 个 “ s”,然后调用substitute方法格式化这个字符串,该方法中指定了s参数值为‘Hello’,最后替换结果是'hello hello hello',也就是说,在格式化字符串中,有多少个“ ssubstitutesHellohellohellohellos”,就替换多少个“$s”。

1、在本例中使用Template格式化字符串,当格式化参数是一个字符串的一部分时,需要用一对大括号({})将格式化参数变量括起来。
#引用string模块中的template类
from string import Template
template1=Template('$s是我最喜欢的编程语言,$s非常容易学习,而且$s功能强大')
print(template1.substitute(s='python'))
#输出结果:
#python是我最喜欢的编程语言,python非常容易学习,而且python功能强大

2、当格式化参数是一个字符串的一部分时,为了和字符串的其他部分区分开,需要用一对大括号将格式化参数变量括起来
template2=Template("${s}stitute")
print(template2.substitute(s='sub'))
#输出结果:
#substitute

template3=Template("$dollar$$相当于多少$pounds")
print(template3.substitute(dollar=20,pounds='英镑'))
#输出结果:
#20相当于多少英镑

二、字符串的format方法

字符串本身也有一个format方法用于格式化当前的字符串。这个format方法和前面讲的格式化操作符(%)不太一样。字符串格式化参数并不是用百分号(%)表示,而不是用一对大括号({}),而且支持按顺序指定格式化参数值和关键字格式化参数。例如,下面的代码通过format方法按顺序为格式化字符串指定了参数值

print("{},{},{}".format(1,2,3))
#输出结果:1 2 3

可以看到,上面的代码在字符串中指定了三对空的大括号,这代表三个格式化参数,不需要指定数据类型,可以向其传递Python语言支持的任何值。通过format方法传入三个值(1,2,3),这三个值会按照顺序替换格式化字符串中的三对空的大括号。


命名格式化参数是指在一对大括号中指定一个名称,然后调用format方法时也要指定这个名称。

print("{a},{b},{c}".format(a=1,c=3,b=3))
#输出结果:1 2 3

上面的代码在三对大括号中分别添加了“a”,“b”,“c”,通过format方法指定了这三个关键字参数的值。可以看到,并没有按顺序指定关键字参数的值。这也是使用关键字参数的好处,只要名字正确,format参数的顺序可以任意指定。当然,顺序方式和关键字参数方式可以混合使用,而且还可以指定顺序方式中格式化参数从format方法提取参数值得顺序,甚至可以取format方法参数值得一部分。

3、本例分别使用一对大括号“{}”、命名格式化参数和顺序格式化参数3中方式格式化字符串
3.1 包含两个大括号,format方法需要按顺序指定格式化参数值
s1="today is {},the teperature is {} degrees."
print(s1.format('saturday',24))
#输出结果:today is saturday,the teperature is 24 degrees.
3.2 包含了2个命名格式化参数,一个是{week},另一个{degree}
s2="today is {week},the teperature is {degree} degrees."
print(s2.format(week='saturday',degree=24))
#输出结果:today is saturday,the teperature is 24 degrees.

3.3 混合了顺序格式化参数和关键字格式化参数两种方式
s3="today is {week},{1},the {0} teperature is {degree} degrees."
print(s3.format('aaaa',1234,week='saturday',degree=24))
#输出结果:today is saturday,1234,the aaaa teperature is 24 degrees.

3.4 定义列表
fullname=['Bill','Gates']
print("Mr {name[1]}".format(name=fullname))
#输出结果:Mr Gates

3.5 导入math模块,访问math模块中的“__math__”变量来获取模块的名字,访问math模块中的pi变量获取PI的值
import math
s5="The {mod.__name__} moudle defines the value {mod.pi} for PI"
print(s5.format(mod=math))
#输出结果:The math moudle defines the value 3.141592653589793 for PI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值