标签:from int orm imp import dict 默认 for python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from string import Template
dict={‘name‘:‘python‘,‘age‘:28}
print(‘我是%(name)s,已有%(age)d年了‘%dict)
#全部替换
str=Template(‘我叫$name,今年$age岁‘)
print(str.substitute(name=‘pyhon‘,age=28))
#部分替换
str=Template(‘我叫${name}ython‘)
print(str.substitute(name=‘pyhon‘))
#显示$
str=Template(‘$$ 我叫$name‘)
print(str.substitute(name=‘pyhon‘))
#使用字典替换
dict={‘name‘:‘python‘,‘age‘:28}
str=Template(‘我叫$name,今年$age岁‘)
print(str.substitute(dict))
# 默认顺序
str=‘{0} {1}‘.format(‘python‘,‘age‘)
print(str)
# 调换顺序
str=‘{1} {0}‘.format(‘python‘,‘age‘)
print(str)
# 指定名字
str=‘this is {name}‘.format(name=‘python‘)
print(str)
# 使用字典
dict={‘name‘:‘python‘,‘age‘:28}
str=‘this is {name} age is {age}‘.format(**dict)
print(str)
# 使用列表
la=[‘python‘,‘age‘]
str=‘this is {0[0]} age is {0[1]}‘.format(la)
print(str)
标签:from int orm imp import dict 默认 for python
原文地址:https://www.cnblogs.com/NiceTime/p/10125339.html