格式化输出的三种方式
1、% 操作符
参考:Python之格式化输出(print %)_python print %-CSDN博客
%s 这些相当于占位符,要跟据 % 后面变量的输出类型来选择占位符的类型
age = 80
name = 'wynyue'
gender = 'boy'
socre = 99.9# 百分号操作符输出
# %s输出字符串,%d输出整数,%.2f输出浮点数并保留两位小数
print('个人信息:%s %d %s %.2f' % (name, age, gender, socre))
运行结果:
2、format()函数
参考:Python format 格式化函数 | 菜鸟教程
同理,{} 作为fomat()的占位符,只需要跟据 .format() 填充确定 {} 的数量即可
并不用考虑变量输出的类型。{} 只能少,不能多
age = 80
name = 'wynyue'
gender = 'boy'
socre = 99.9#format()函数输出
print('个人信息:{} {} {} {}' .fotmat(name, age, gender, socre))
3、f-strings
参考:python | f-string_python f string-CSDN博客
f-strings的含义是 format()-字符串
在字符串前面加一个 f ,把字符串中 {} 内的字符转换为变量
不加 f 时
加了 f 后,{} 内字符的含义就发生了变化