name = 'Xi4or0uji' print'My name is %s' %name My name is Xi4or0uji
使用标准库中的模板字符串
1 2 3 4 5
from string import Template name = 'Xi4or0uji' s = Template('My name is $name') print s.substitute(name=name) My name is Xi4or0uji
利用format格式化字符串
直接格式化字符串
1 2
print'My name is {}'.format('Xi4or0uji') My name is Xi4or0uji
指定位置
1 2 3 4
print'this is a {0}, that is a {1}'.format('cat','dog') this is a cat, that is a dog print'this is a {1}, that is a {0}'.format('cat','dog') this is a dog, that is a cat
设置参数
1 2
print'hello {adj} {name}'.format(adj='little',name='h4ck3r') hello little h4ck3r
百分比格式
1 2
print'{:.2%} off'.format(0.20) 20.00% off
获得数组的键值
1 2
print'{arr[2]}'.format(arr=[0,1,2,3,4,5]) 2
还有很多,不举例了
一个小栗子
1 2 3 4 5 6
config = {'SECRET_KEY':'lolololol'} classUser(object): def__init__(self,name): self.name = name user = User('Xi4or0uji') print'hello {name}'.format(name=user.__class__.__init__.__globals__)
@bp_auth.route('/flag') @login_check defget_flag(): if(g.user.username=="admin"): with open(os.path.dirname(__file__)+'/flag','rb') as f: flag = f.read() return flag return"Not admin!!"
评论