格式化输出.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # -*- coding: UTF-8 -*-
  2. ##============ 一个参数 ============###
  3. tpl = 'i am %s' % 'xdc'
  4. print(tpl)
  5. ##============ 多个参数 ============###
  6. tpl = 'i am %s age %d' % ('xdc', 21) # 多个参数要括号
  7. print(tpl)
  8. ###============ 传 ============###
  9. tpl = 'i am %(name)s age %(age)d' % {'name':'xdc', 'age': 21}
  10. print(tpl)
  11. ###============ ============###
  12. tpl = 'i am %.2f ' % 99.97640
  13. print(tpl)
  14. tpl = 'i am %(pp).2f' % {'pp': 8848.12134}
  15. print(tpl)
  16. tpl = 'i am %.2f%% ' % 88.95134 # 输出 % 号 (%%)
  17. print(tpl)
  18. ##============ format ======================== ============###
  19. tpl = "i am {}, age {}, {}"
  20. r = tpl.format("xdc", 21, 'didi',12)
  21. print(r)
  22. tpl = "i am {}, age {}, {}"
  23. r = tpl.format(*["xdc", 21, 'didi',12]) # 多传不会报错,少传会报错
  24. print(r)
  25. a, *b = ["xdc", 21, 'didi']
  26. print(a)
  27. print(b)
  28. *a, b = ["xdc", 21, 'didi']
  29. print(a)
  30. print(b)
  31. tpl = "i am {0}, age {1}, {0}" # 也可以通过索引的方式传
  32. print(tpl.format('xdc',1))
  33. tpl = "i am {name}, age {age}, {name}" # 通过关键字参数传
  34. print(tpl.format(name='xdc',age=2))
  35. tpl = "i am {name}, age {age}, {name}" # 通过关键字参数传
  36. print(tpl.format(**{'name': 'xdc', 'age': 2}))
  37. tpl = "i am {0[2]}, age {1[0]}, {1[2]}" # 索引取
  38. print(tpl.format([1,2,3], [4,5,6]))
  39. tpl = "i am {:s}, age {:d}, {:.2f}" # 格式化 :s :d :f
  40. print(tpl.format('xdc', 18, 89.1))
  41. tpl = "i am {:s}, age {:d}".format(*['xdc',18])
  42. print(tpl)
  43. tpl = "i am {name:s}, age {age:d}".format(name='xdc',age= 19)
  44. print(tpl)
  45. tpl = "i am {name:s}, age {age:d}" # 类型与名字都指定
  46. print(tpl.format(**{'name':'xdc','age':12}))
  47. tpl = "numbers: {0:b}, {0:o}, {0:d}, {0:x}, {0:X},"
  48. # {:b} -> 二进制,{:o} -> 八进制, {:d} -> 十进制,
  49. # {:x} -> 小写十六进制, {:X} -> 大写十六进制
  50. print(tpl.format(15))
  51. tpl = "numbers: {num:b}, {num:o}, {num:d}, {num:x}, \
  52. {num:X}," # 通过关键值传递 \ 换行,与linux中作用类似
  53. print(tpl.format(num=15))
  54. tpl = "{0:<6}---{0:<8}---{0:>13}" # <6左对齐6格不够用空格补齐, >13 右对齐13格
  55. print(tpl.format('123'))
  56. tpl = "{0:=<6}---{0:<8}---{0:>13}" # =<6左对齐6格不够用 = 补齐
  57. print(tpl.format('123'))
  58. tpl = "{0:=^6}---{0:^8}---{0:^13}" # ^ 居中
  59. print(tpl.format('123'))
  60. from string import Template
  61. user = 'xdc'
  62. s = Template("hello $name")
  63. print(s.substitute(name=user))
  64. name = '徐大虫'
  65. age = 21
  66. s = f'{name}今年{age}岁'
  67. print(s)
  68. def to_lowercase(inp):
  69. return inp.lower()
  70. name = 'XDc'
  71. s = f"{to_lowercase(name)} is string" # 可以使用函数的方式传递
  72. print(s)
  73. tpl =(
  74. 'xdc'
  75. 'xc')
  76. print(tpl) # 换行,代码美观。输出是一行。
  77. msg = """
  78. kafaka
  79. xdc
  80. sdf
  81. """
  82. print(msg)