# 字符串类型格式化
formats = "姓名:{},年龄:{},性别:{}".format("马云","16","男")
print(formats)
# 姓名:马云,年龄:16,性别:男
formats = "姓名:{2},年龄:{1},性别:{0}".format("马云","16","男")
#曹中的数字代表format的中的第几个参数
print(formats)
# 姓名:男,年龄:16,性别:马云
![](https://img-blog.csdnimg.cn/20190506160554568.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTE1Mjc0Mw==,size_16,color_FFFFFF,t_70)
Str = "{0:=^20}".format("PYTHON")
print(Str)
# =======PYTHON=======
Str = "{0:\>20}".format("PYTHON")
print(Str)
# \\\\\\\\\PYTHON
Str = "{:10}".format("Python")
print(Str)
# Python右边长度为10的空格
![](https://img-blog.csdnimg.cn/20190506155630736.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTE1Mjc0Mw==,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20190506160844807.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTE1Mjc0Mw==,size_16,color_FFFFFF,t_70)
num = "{0:.2f}".format(123456.789132)
print(num)
# 123456.79
num = "{0:b},{0:c},{0:d},{0:o},{0:x},{0:X}".format(42500)
# {0:b}二进制格式,{0:c}Unicode编码形式,{0:d}十进制表示形式,{0:o}八进制,{0:x}十六进制,{0:X}大写的十六进制
print(num)
# 1010011000000100,ꘄ,42500,123004,a604,A604
num = "{0:e},{0:E},{0:F},{0:%}".format(45.6)
# {0:e}科学计数法e表示,{0:E}科学计数法E表示,{0:F}通常非科学计数法
print(num)
# 4.560000e+02,4.560000E+02,456.000000,45600.000000%