标签:pre odi 默认 you 类型转换 十六 输出 综合 mat
input 用户交互函数name = input("your name:")
print(input("your name:"))
your name:dwl
常用占位符
%d 整数
%f 浮点数
%s 字符串
%x 十六进制整数
使用方法:
tpl = "i am %s" % "alex" ## i am alex
tpl = "i am %s age %d" % ("alex", 18) ## i am alex age 18
tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18} # i am alex age 18 (注意其中占位符的写法,使用key value的方式)
tpl = "percent %.2f" % 99.97623 #percent 99.98‘ .2f% 表示小数点后两位四舍五入
tpl = "i am %(pp).2f" % {"pp": 123.425556, } # i am 123.43
Format方法:
tpl = "i am {}, age {}, {}".format("seven", 18, ‘alex‘)
##i am seven, age 18, alex 注意.format的书写位置在“”后面,文字按顺序补全
tpl = "i am {}, age {}, {}".format(*["seven", 18, ‘alex‘])
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
##文字顺序 0,1,2开始
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
综合练习:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
name=input("name:")
age=int(input("age:")) ##使用int 字符类型转换函数 为使跟%d 类型相匹配
job=input("job:")
salary=input("sarlary:")
info=‘‘‘---------info of %s ---------
name:%s
age:%d
job:%s
salary:%s
‘‘‘%(name,name,age,job,salary)
print(info)
info2=‘‘‘---------info2 of {_name} ---------
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}‘‘‘.format(_name=name,_age=age,_job=job,_salary=salary)
print(info2)
1,Dvops python(input用户交互,格式化文本输出)
标签:pre odi 默认 you 类型转换 十六 输出 综合 mat
原文地址:https://blog.51cto.com/1014810/2519687