Python的文本常量与字符串模板之string库

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Python的⽂本常量与字符串模板之string库
⽬录
⼀、前⾔
⼆、⾸字母⼤写
三、字符串模板
四、⾼级模板
五、format⽤法
六、进阶⽤法
七、⾼阶⽤法
⼀、前⾔
在程序中,有很多⾼效率的字符串处理⽅式,如果开发者能够完全掌握这些⾼效的字符串处理,往往在开发者也能事半功倍。

⽐如针对于字符串的处理,也是⾃然语⾔处理的基础知识。

⽽python3中,处理字符串的库为:string。

本篇将详细介绍各种字符串的⾼效处理⽅式。

⼆、⾸字母⼤写
对于英⽂单词组成的字符串来说,很多时候,我们需要对英⽂的⾸字母进⾏⼤写的变更。

如果没有了解其⾼效率的函数,⼀般我们都通过循环,判断空格,取空格后⼀位的字母,判断其在ASCII中的编码后,取其⼤写替换掉该位置的字符串。

但是,python3中有⼀个函数可以直接将⾸字母⼤写,该函数为capwords()。

下⾯,我们来通过⼀⼩段代码实现⾸字母⼤写的字符串变更。

import string
s = "When he shewed the riches of his glorious kingdom and the honour of his excellent majesty many days, even an hundred and fourscore days"
print("原始字符串")
print(s)
result = string.capwords(s)
print("⾸字母⼤写字符串")
print(result)
运⾏之后,我们会得到全⼤写⾸字母字符串:
三、字符串模板
在string库中,字符串模板函数为string.Template(),它可以⽤来拼接字符串。

⽰例代码如下:
import string
values = {
"name": "liyuanjing",
"age": "13",
}
s = """My name is : $name
I am $age years old
"""
template_str = string.Template(s)
print(template_str.substitute(values))
这⾥,我们使⽤字符串模板string.Template,然后通过函数substitute()进⾏字符串替换。

不过,这⾥有可能替换时values字典中没有对应的key怎么办?string库还给我们提供了⼀个函数safe_substitute()。

import string
values = {
"name": "liyuanjing",
"age": "13",
}
s = """My name is : $name
I am $age years old
$work
"""
template_str = string.Template(s)
print(template_str.safe_substitute(values))
因为字典没有对应的值进⾏替换,所以会保留原始的字符串数据。

效果如下:
四、⾼级模板
上⾯的模板使⽤⽅法是string库默认提供的规则体系。

其实,我们还可以⾃定义模板的使⽤匹配⽅法,具体代码如下:
import string
class MyTemplate(string.Template):
delimiter = '@'
idpattern = '[a-z]+_[0-9]+'
values = {
"name_1": "liyuanjing",
"age_1": "13",
}
s = """My name is : @name_1
I am @age_1 years old
@work_1
"""
template_str = MyTemplate(s)
print(template_str.safe_substitute(values))
这⾥,delimiter代表需要匹配的符号,默认符号"$",博主替换成了‘@'。

其次,idpattern是values对应的key名字规则,这⾥⽤正则表达式规定,⽐如是"字符串_数字"。

运⾏之后,效果如下:
五、format⽤法
基本⽤法
有过其他语⾔基础的都应该或多或少接触过format字符串替换。

这⾥,我们直接来看看其基本的使⽤⽅式:
print("My name is {}".format("liyuanjing"))#⼤括号匹配,按顺序依次填充
print("My {1} is {0}".format("liyuanjing","name"))#数字匹配,按位置依次填充
print("My {name} is {tom}".format(tom="liyuanjing",name="name"))#关键字匹配,按关键字填充
运⾏之后,效果如下:
六、进阶⽤法
format函数不仅可以匹配替换字符串,还可以通过它对其⽂本,或者取⼩数某⼏位等等。

下⾯,我们来看看这些⽤法如何实现。

print('{} and {}'.format('tom', 'Jerry'))
print('{:10s}'.format('*')) # 默认左对齐
print('{:>10s}'.format('*')) # 右对齐
print('{:^10s}'.format('*')) # 中间对齐
print('{:<10s}'.format('*')) # 左对齐
print('{} is {:.2f}'.format(3.411592653, 3.1415926))#取2位⼩数
values = {
"name_1": "liyuanjing",
"age_1": "13",
}
s = """My name is : {name_1}
I am {age_1} years old
"""
print(s.format(**values))
注释已经⾮常详细,这⾥不在赘述。

效果如下:
七、⾼阶⽤法
format除了能做上⾯这些事情之外,还可以转换进制以及ASCII码符号等等。

下⾯,我们来实现这些⾼阶⽤法。

print('{:b}'.format(8))#:b转换为⼆进制
print('{:c}'.format(200))#:c转换Unicode字符串
print('{:d}'.format(111))#:d转换⼗进制
print('{:o}'.format(8))#:o转换⼋进制
print('{:x}'.format(32))#:x转换⼗六进制
print('{:e}'.format(32))#:e转换幂符号
print('{:%}'.format(0.32))#:%转换百分值
print('{:n}'.format(32000000000))#:n就是数值
print('{:g}'.format(32000000000))#:n也是数值,不过特别⼤时转换为幂科学计数
运⾏之后,效果如下:
到此这篇关于Python的⽂本常量与字符串模板string库的⽂章就介绍到这了,更多相关Python string库内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。

相关文档
最新文档