Python 常用的占位符

hold_space1 = "I am  %s" % 97   #字符串占位符,把97当作字符串处理
print(hold_space1)   # I am  97

hold_space2 = "I am  %c%c" % (20013,22269)   #字符占位符,输出数字对应的Unicode编码对应的字符
print(hold_space2)   # I am  中国

hold_space3 = "I am  %d" % 97   #整数占位符,把97当作整数
print(hold_space3)   # I am  97

hold_space4 = "I am  %f" % 97   #浮点数占位符,把97转换成浮点数
print(hold_space4)   # I am  97.000000

hold_space5 = "I am  %(name)s, %(age)d years old" % { "age":5000,"name":"汉字"}
                     #带名字的占位符,后面的顺序
print(hold_space5)   # I am  汉字, 5000 years old

hold_space6 = "I am  {name}, {age} years old" .format (name="汉字", age=5000)
                     # 这是另一种格式化的方式,具体细节不做详细介绍了
print(hold_space6)

 

 

你可能感兴趣的:(Python)