使用f-string进行格式化字符串输出

避坑记录

当在字符串前面使用f-string的时候

就不要使用%了,不然会被当成占位符来处理,应该直接使用变量名:输出格式就好

学习测试代码

'''
小明的成绩从去年的72分提升到了今年的85分,
请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',
只保留小数点后1位:
'''


def practice():
    last_score = 72
    now_score = 85

    reslut = (now_score - last_score) / last_score * 100
    string = f'从去年到现在,小明的成绩从{
      
      last_score}提高到了{
      
      now_score}。小明的成绩提高了{
      
      reslut:.1f}%'
    print(string)


if __name__ == '__main__':
    practice()

猜你喜欢

转载自blog.csdn.net/weixin_44943389/article/details/132979809
今日推荐