Fork me on GitHub

百分号使用

Posted on 2019-03-27 21:51  攀一座山  阅读(251)  评论(0编辑  收藏  举报
# 百分号字符串拼接 %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 am\033[44;1m%(name)+60s\033[0m ' %{'name':'lcy'}
print(t)

# 向字符串插入符号连接
print(":".join(("root","x",'0','0')))
print("root","x",'0','0',sep=':')

Copyright © 2024 攀一座山
Powered by .NET 8.0 on Kubernetes