# 创建字符串模板
template = Template("My name is $name and I am $age years old.")
# 使用substitute方法进行替换
result = template.substitute(name="Alice", age=25)
print(result) # 输出:My name is Alice and I am 25 years old.
[code]
在上面的示例中,我们首先导入了Template类,然后创建了一个字符串模板对象。该模板中包含了带有占位符$name和$age的字符串。然后,我们使用substitute方法来将占位符替换为实际的值,并将结果保存在变量result中,最后打印输出结果。