百分号使用

# 百分号字符串拼接 %s接收一切,%d只接收数字。
mm='i am %s my girl is %s' %("lcy","lwz")
print(mm)

nn=' %s is %d '%("lwz",250)
print(nn)

nn=' %(name) is %(num) '%{"name":"lwz","num":250}

# 最佳使用方式
name1="lcy"
name2="lwz"
mm='i am %s my girl is %s' %(name1,name2)
print(mm)

# 打印浮点数
tp1='percent %f'%99.987987987
print(tp1)
# 输出percent 99.987988

tp1='percent %.2f'%99.987987987
print(tp1)
# 输出percent 99.99

# 打印百分比 保留小数点后三位
tp1='percent %.3f %%'%99.987987987
print(tp1)
# 加颜色 扩充字符右对齐(-左对齐)
t=' i am33[44;1m%(name)+60s33[0m ' %{'name':'lcy'}
print(t)

# 向字符串插入符号连接
print(":".join(("root","x",'0','0')))
print("root","x",'0','0',sep=':')
原文地址:https://www.cnblogs.com/panyizuoshan/p/10611154.html