String类



自有的常用属性,可用于正则化

String类

字符串大小写变换

capwords(s, sep=None):
    """capwords(s [,sep]) -> string

测试:

from string import  capwords
print(capwords('aFe'))

结果:Afe



Template模块:

源代码表示要先输入模板template

class Template(metaclass=_TemplateMetaclass):
    """A string class for supporting $-substitutions."""

    delimiter = '$'
    idpattern = r'[_a-z][_a-z0-9]*'
    flags = _re.IGNORECASE

    def __init__(self, template):
        self.template = template
案例1:substitude方法

from string import Template
s = Template('There  ${moneyType} is  ${money}')
print(s.substitute(moneyType='Dollar', money=12))

结果:There  Dollar is  12


案例2:安全替代,只改变其中一个,但是变成了string类型,没什么用

b = Template(s.safe_substitute(moneyType='Yuan'))
print(b.substitute(money=100))

结果:There  Yuan is  100