Python f-string方法有个非常便捷的实现格式化百分数的操作方法。方法与浮点数格式化类似,但是要用%代替结尾的f。它会将原始数值乘以100并显示成有百分符号的固定格式。精度一样也是可以设定的。
>>> total = 87
>>> true_pos = 34
>>> perc = true_pos / total
>>> perc
0.39080459770114945
>>> f"Percentage of true positive: {perc:%}"
'Percentage of true positive: 39.080460%'
>>> f"Percentage of true positive: {perc:.2%}"
'Percentage of true positive: 39.08%'