0x01 使用“%”格式化字符串输出(最常见)
![9798c64405b0ad7bb6e1afdeb2045894.png](https://img-blog.csdnimg.cn/img_convert/9798c64405b0ad7bb6e1afdeb2045894.png)
强烈的C语言表示风格,这种写法不是pythonic风格!
0x02 使用format格式化字符串输出,不指定参数名
![ce7858407f6231a5f8ad7adf7b87c4e6.png](https://img-blog.csdnimg.cn/img_convert/ce7858407f6231a5f8ad7adf7b87c4e6.png)
0x03 使用format格式化字符串输出,不按位置顺序映射
![317384480fa9e235adbc14cbdfe2940d.png](https://img-blog.csdnimg.cn/img_convert/317384480fa9e235adbc14cbdfe2940d.png)
不按位置顺序进行映射,参数可以使用多次
![7eb2d59b4e622cdf5a0118f5e7d08798.png](https://img-blog.csdnimg.cn/img_convert/7eb2d59b4e622cdf5a0118f5e7d08798.png)
0x04 使用format格式化字符串输出,通过下标映射
![babf4988a9085ea3efadf36e171e8bb8.png](https://img-blog.csdnimg.cn/img_convert/babf4988a9085ea3efadf36e171e8bb8.png)
0x05 使用format格式化字符串输出,通过关键字映射
![740b7b33c77959cf48f0701d83a51159.png](https://img-blog.csdnimg.cn/img_convert/740b7b33c77959cf48f0701d83a51159.png)
0x06 使用format格式化字符串输出,通过字典的key映射
![6e142af3b7c5706ff1228b545638cbb9.png](https://img-blog.csdnimg.cn/img_convert/6e142af3b7c5706ff1228b545638cbb9.png)
0x07 使用format格式化字符串输出,通过对象的属性映射
![85bac29294578066b8bc6a8eae4b890c.png](https://img-blog.csdnimg.cn/img_convert/85bac29294578066b8bc6a8eae4b890c.png)
0x08 使用format格式化字符串输出,通过 * 和 ** 形式映射
![e09fb0e7db95592e68dad4f005c6b6a2.png](https://img-blog.csdnimg.cn/img_convert/e09fb0e7db95592e68dad4f005c6b6a2.png)
0x09 使用字符串模板格式化字符串输出
![0e1a6136c29f93d807e816e4d514ab89.png](https://img-blog.csdnimg.cn/img_convert/0e1a6136c29f93d807e816e4d514ab89.png)
0x10 使用魔法参数格式化字符串输出
![59ae020d5127b8e67b07b035d2424374.png](https://img-blog.csdnimg.cn/img_convert/59ae020d5127b8e67b07b035d2424374.png)
魔法参数的用法是在Python3.6版本引入的
其用法是在字符串前加 "f" 前导符,字符串中间则用花括号"{}"包裹其它字符串变量
这种方式格式化字符串方式可读性比较好,但向前兼容性差。
总结
写了十种格式化字符串的方法,强烈推荐使用format各种变体!
出于兼容性考虑,不建议使用魔法参数格式化字符串,即使它可读性好,性能强!
最后
喜欢我的朋友们,麻烦点一波关注,谢谢!