python格式化输出
作者:互联网
占位符%
%s (str类型占位)
%d(digit,int类型占位)
案例:
name = input('请输入您的姓名:') age = input('您的年龄:') job = input('您的工作:') hobbie = input('您的爱好:') mag = '''-------------info of %s------------- name:%s age:%d job:%s Hobbie:%s -------------END------------- ''' % (name, name, int(age), job, hobbie) print(mag)View Code
如果只是单纯的想输出%,可在%前加转义%解决(%%)
案例:
name = input('请输入姓名:') day = int(input('学习天数:')) days = 51 mag = '''--------学习进度-------- 姓名:%s 学习天数:%d --------学习进度为%d%%-------- ''' % (name, day, day * 100 / days) print(mag)View Code
format格式化输出
name = input('请输入您的姓名:') age = input('您的年龄:') say = '我叫{},今年{}岁'.format(name, age) say1 = '我叫{1},今年{0}岁'.format(name, age) say2 = '我叫{names},今年{ages}岁'.format(names=name, ages=age) print(say)View Code
标签:输出,格式化,name,format,python,age,int,mag,input 来源: https://www.cnblogs.com/zhuzl/p/11106742.html