字符串输出

  1. a1 = float(input('a1 = '))
  2. b2 = float(input('b2 = '))
  3. # 用字符串输出
  4. print(a1, '+', b2, '=', (a1 + b2))
  5. print(a1, '-', b2, '=', (a1 - b2))
  6. print(a1, '*', b2, '=', (a1 * b2))
  7. print(a1, '/', b2, '=', (a1 / b2))
  8. print(a1, '//', b2, '=', (a1 // b2))
  9. print(a1, '%', b2, '=', (a1 % b2))
  10. print(a1, '**', b2, '=', (a1 ** b2))
  11. # 1.2 + 3.4 = 4.6
  12. # 1.2 - 3.4 = -2.2
  13. # 1.2 * 3.4 = 4.08
  14. # 1.2 / 3.4 = 0.35294117647058826
  15. # 1.2 // 3.4 = 0.0
  16. # 1.2 % 3.4 = 1.2
  17. # 1.2 ** 3.4 = 1.858729691979481

占位符格式化输出

  1. a1 = float(input('a1 = '))
  2. b2 = float(input('b2 = '))
  3. # 占位符格式化输出
  4. # float占位符%f
  5. # int占位符%d
  6. # %号后面可以定义保留小数点后几位
  7. # %f或%d后面可以接固定字符串
  8. print('%f + %f = %f' % (a1, b2, a1 + b2))
  9. print('%f - %f = %.1f' % (a1, b2, a1 - b2))
  10. print('%f * %f = %.2f' % (a1, b2, a1 * b2))
  11. print('%f / %f = %.3f' % (a1, b2, a1 / b2))
  12. print('%.1f // %.2f = %.3f' % (a1, b2, a1 // b2))
  13. print('%.2f %% %.2f = %.2f' % (a1, b2, a1 % b2))
  14. print('%f这里 ** %f可以加 = %f字符串' % (a1, b2, a1 ** b2))
  15. # a1 = 1.2
  16. # b2 = 3.4
  17. # 1.200000 + 3.400000 = 4.600000
  18. # 1.200000 - 3.400000 = -2.2
  19. # 1.200000 * 3.400000 = 4.08
  20. # 1.200000 / 3.400000 = 0.353
  21. # 1.2 // 3.40 = 0.000
  22. # 1.20 % 3.40 = 1.20
  23. # 1.200000这里 ** 3.400000可以加 = 1.858730字符串

格式化输出

  1. a1 = float(input('a1 = '))
  2. b2 = float(input('b2 = '))
  3. # 控制小数位数,在变量后增加:.2f
  4. print(f'{a1}+{b2}={a1 + b2}')
  5. print(f'{a1}-{b2}={a1 - b2}')
  6. print(f'{a1}*{b2}={a1 * b2}')
  7. print(f'{a1}/{b2}={a1 / b2}')
  8. print(f'{a1}//{b2}={a1 // b2}')
  9. print(f'{a1}这里%{b2}可以加={a1 % b2}字符串')
  10. print(f'{a1}**{b2}={a1 ** b2:.2f}')