格式化字符串在编码中经常使用,总结如下:
①占位符:%s
②format:{}.format()
③f-string:f"…{}…" (需python 3.6之后)
示例:
%s
test_str = 'hello %s' % (world) # hello world
format
test_str = 'hello {}'.format(world) # hello world
f-string
test_str= f"{1+2}" # 3
good = {'goodsname': 'vitamin', 'goodsnum': 20}
test_str = f"goodsname is {good['goodsname']}, number {good['goodsnum']}."
#goodsname is vitamin, number 20