python对于格式的三种控制方法
一、f‘xxx {a}’ 前面加 f 后面变量加上大括号,要有变量,且引用的时候要被扩起来 print(f’{xxx}’) example 01 >> > event = 'tornado' >> > year = 2005.123456 >> > print ( f 'There is an {event} in {year:.3f}' ) #保留小数的位数 There is an tornado in 2005.123 【d保证的是整数,f保证的是小数】 example 02 a = str ( input ( 'please input the first name: ' )) b = str ( input ( 'please input the second name: ' )) print ( f '{a} and {b} are helping each other.' ) please input the first name: wyb please input the second name: xz wyb and xz are helping each other. 二、print(’{} xxx {}’.format(a,b)) *是最常用的格式输出 print(’{} xxx {}’.format(a,b)) 位置指定时,第一个是0 第二个是1 a =