格式化类:%、format()

拼接类: +、()、join()

  1. # 拼接并换行显示
  2. import sys
  3. print("\n".join(sys.path))

填值类:f-string

  1. name = 'zaygee'
  2. myname = 'python_test'
  3. print("--------------格式化类----------------")
  4. print('i am %s,i am studying %s' % (name, myname))
  5. print("i am {0},i am studying {1}".format(name, myname))
  6. print("-------------拼接类--------------------")
  7. print(name + ' ' + myname)
  8. list_info = ['zaygee', 'python']
  9. print(' '.join(list_info))
  10. print("-------------------填值类-------------------")
  11. print(f'i am {name},i am studying {myname}')
  12. #--------------格式化类----------------
  13. #i am zaygee,i am studying python_test
  14. #i am zaygee,i am studying python_test
  15. #-------------拼接类--------------------
  16. #zaygee python_test
  17. #zaygee python
  18. #-------------------填值类-------------------
  19. #i am zaygee,i am studying python_test

字符串对齐方式

  1. print("{:<10}".format("a")) # :< 为左对齐
  2. print("{:>10}".format("a")) # :< 为右对齐
  3. print("{:^10}".format("a")) # :^ 居中对齐
  4. # a
  5. # a
  6. # a
  7. for x in range(10):
  8. # 不指定默认左对齐
  9. print("{:2d} {:3d} {:5d}".format(x, x*x, x*x*x))
  10. # 0 0 0
  11. # 1 1 1
  12. # 2 4 8
  13. # 3 9 27
  14. # 4 16 64
  15. # 5 25 125
  16. # 6 36 216
  17. # 7 49 343
  18. # 8 64 512
  19. # 9 81 729