字符串格式化有2种方式:
1,%
#!/usr/bin/env python3 #-*- coding:utf-8 -*- import datetime name="shasha" timenow = datetime.date.today() ss = '%s周末了,给家人亲手泡一杯茶吧%s'%(name,timenow) print(ss) 备注:%s 后面的参数可以是整数,str,float; 如果是%d 后面必须是整数%f 后面必须是float
2, .format
#不指定顺序 按默认顺序 a1 = '{}{}{}'.format(1,2,1) print(a1) #设置指定位置 a2 = '{1}{0}'.format(1,2) print(a2) a3 = '{1}{0}{0}'.format(1,2) print(a3) #设置参数 a4 = '网站名:{}'.format('百度') print(a4) sql = 'insert into score(id,name,grade) values({id},{name},{grade})' a6 = sql.format(id = 1,name = 'haha',grade = 88) print(a6)