task1_1
print('hey,u') hey,u print('hey','u') hey u x,y,z=1,2,3 print(x,y,z) 1 2 3 print('x = %d, y = %d, z = %d' %(x,y,z)) x = 1, y = 2, z = 3 print('x = {}, y = {}, z = {}'.format(x,y,z)) x = 1, y = 2, z = 3 print(f'x = {x}, y = {y}, z = {z}') x = 1, y = 2, z = 3 print(x) 1 print(y) 2 print(z) 3 print(x, end=' ') 1 print(y, end=' ') 2 print(z) 3
task1_2
x1,y1=1.2,3.57 x2,y2=2.26,8.7 print('{:-^40}'.format('输出1')) ------------------输出1------------------- print('x1 = {}, y1 = {}'.format(x1, y1)) x1 = 1.2, y1 = 3.57 print('x2 = {}, y2 = {}'.format(x2, y2)) x2 = 2.26, y2 = 8.7 print('{:-^40}'.format('输出2')) ------------------输出2------------------- print('x1 = {:.1f}, y1 = {:.1f}'.format(x1, y1)) x1 = 1.2, y1 = 3.6 print('x2 = {:.1f}, y2 = {:.1f}'.format(x2, y2)) x2 = 2.3, y2 = 8.7 print('{:-^40}'.format('输出3')) ------------------输出3------------------- print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1, y1)) x1 = 1.2 , y1 = 3.6 print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2, y2)) x2 = 2.3 , y2 = 8.7 print('{:-^40}'.format('输出3')) ------------------输出3------------------- print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1, y1)) x1 = 1.2, y1 = 3.6 print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2, y2)) x2 = 2.3, y2 = 8.7
task1_3
name1, age1 = 'Bill', 19 name2, age2 = 'Hellen', 18 title = 'Personnel Information' print(f'{title:=^40}') =========Personnel Information========== print(f'name: {name1:10} age: {age1:3}') name: Bill age: 19 print(f'name: {name2:10} age: {age2:3}') name: Hellen age: 18 print(40*'=') ========================================
实验结论:
1,用print输出字符或字符串时需加'',输出数字与参数时则不需。print中用“,”分隔的数据,在输出后由空格分开。将x,y,z=1,2,3变为x=1,y=2,z=3可用c方法,format,f-string来实现
2,print({:-^x}.format('A'))可实现宽度占x列,A居中,空白由-补齐。{:.1f}可让数字保留一位小数。
3,print(f'{A:=^x}')一样可实现输出宽度占x列,A居中对齐,用=填充空白
标签:x1,format,x2,y1,实验,print,y2 From: https://www.cnblogs.com/qkzt/p/17216373.html