'''字符串格式化''' msg = ' I am %s, my hobby is %s!' % ('mike', 'basketball') #%s能接收所有格式 print(msg) msg1 = 'I am %s, age %d!' % ('mike', 18) #%d接收整型数值 print(msg1) tp1 = 'percent %f' % 99.9762345 #%f接收浮点型数值,.表示小数点,后面跟的数字表示展示几位小数,而且无论是展示多少位都会取四舍五入 print(tp1) tp2 = 'percent %.0f' % 99.9762345 print(tp2) tp3 = 'I am %(name)s age %(age)d' % {'name':'alex', 'age':18} #键值对的方式进行拼接 print(tp3) tp4 = 'I am {}, age {}, {}'.format('alex', 18, 'hello man!') print(tp4) tp5 = 'I am {2}, age {1}, {0}'.format('hello man!', 18, 'alex') print(tp5) tp6 = 'I am {name}, my hobby is {hobby}'.format(name='mike', hobby='basketball') print(tp6) tp7 = 'I am {name}, my hobby is {hobby}'.format(**{'name':'mike', 'hobby':'basketball'}) print(tp7) tp8 = 'I am {:s}, age {:d}'.format(*['mike', 18]) print(tp8)