| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- # -*- coding: UTF-8 -*-
- ##============ 一个参数 ============###
- tpl = 'i am %s' % 'xdc'
- print(tpl)
- ##============ 多个参数 ============###
- tpl = 'i am %s age %d' % ('xdc', 21) # 多个参数要括号
- print(tpl)
- ###============ 传 ============###
- tpl = 'i am %(name)s age %(age)d' % {'name':'xdc', 'age': 21}
- print(tpl)
- ###============ ============###
- tpl = 'i am %.2f ' % 99.97640
- print(tpl)
- tpl = 'i am %(pp).2f' % {'pp': 8848.12134}
- print(tpl)
- tpl = 'i am %.2f%% ' % 88.95134 # 输出 % 号 (%%)
- print(tpl)
- ##============ format ======================== ============###
- tpl = "i am {}, age {}, {}"
- r = tpl.format("xdc", 21, 'didi',12)
- print(r)
- tpl = "i am {}, age {}, {}"
- r = tpl.format(*["xdc", 21, 'didi',12]) # 多传不会报错,少传会报错
- print(r)
- a, *b = ["xdc", 21, 'didi']
- print(a)
- print(b)
- *a, b = ["xdc", 21, 'didi']
- print(a)
- print(b)
- tpl = "i am {0}, age {1}, {0}" # 也可以通过索引的方式传
- print(tpl.format('xdc',1))
- tpl = "i am {name}, age {age}, {name}" # 通过关键字参数传
- print(tpl.format(name='xdc',age=2))
- tpl = "i am {name}, age {age}, {name}" # 通过关键字参数传
- print(tpl.format(**{'name': 'xdc', 'age': 2}))
- tpl = "i am {0[2]}, age {1[0]}, {1[2]}" # 索引取
- print(tpl.format([1,2,3], [4,5,6]))
- tpl = "i am {:s}, age {:d}, {:.2f}" # 格式化 :s :d :f
- print(tpl.format('xdc', 18, 89.1))
- tpl = "i am {:s}, age {:d}".format(*['xdc',18])
- print(tpl)
- tpl = "i am {name:s}, age {age:d}".format(name='xdc',age= 19)
- print(tpl)
- tpl = "i am {name:s}, age {age:d}" # 类型与名字都指定
- print(tpl.format(**{'name':'xdc','age':12}))
- tpl = "numbers: {0:b}, {0:o}, {0:d}, {0:x}, {0:X},"
- # {:b} -> 二进制,{:o} -> 八进制, {:d} -> 十进制,
- # {:x} -> 小写十六进制, {:X} -> 大写十六进制
- print(tpl.format(15))
- tpl = "numbers: {num:b}, {num:o}, {num:d}, {num:x}, \
- {num:X}," # 通过关键值传递 \ 换行,与linux中作用类似
- print(tpl.format(num=15))
- tpl = "{0:<6}---{0:<8}---{0:>13}" # <6左对齐6格不够用空格补齐, >13 右对齐13格
- print(tpl.format('123'))
- tpl = "{0:=<6}---{0:<8}---{0:>13}" # =<6左对齐6格不够用 = 补齐
- print(tpl.format('123'))
- tpl = "{0:=^6}---{0:^8}---{0:^13}" # ^ 居中
- print(tpl.format('123'))
- from string import Template
- user = 'xdc'
- s = Template("hello $name")
- print(s.substitute(name=user))
- name = '徐大虫'
- age = 21
- s = f'{name}今年{age}岁'
- print(s)
- def to_lowercase(inp):
- return inp.lower()
- name = 'XDc'
- s = f"{to_lowercase(name)} is string" # 可以使用函数的方式传递
- print(s)
- tpl =(
- 'xdc'
- 'xc')
- print(tpl) # 换行,代码美观。输出是一行。
- msg = """
- kafaka
- xdc
- sdf
- """
- print(msg)
|