1.多行格式化输出
(1)
1 # Author:larlly 2 name = input("input your name :") 3 age = input("input your age :") 4 job = input("input your job :") 5 salary = input("input your salary :") 6 7 info = ''' 8 ----------info of {_name}-------------- 9 Name:{_name}10 Age:{_age}11 job:{_job}12 salary:{_salary}13 '''.format(_name=name,14 _age=age,15 _job=job,16 _salary=salary)17 print(info)
(2)
# Author:larllyname = input("input your name :")age = input("input your age :")job = input("input your job :")salary = input("input your salary :")info = '''----------info of %s--------------Name:%sAge:%sjob:%ssalary:%s''' %(name,name,age,job,salary)print(info)
(3)
1 # Author:larlly 2 name = input("input your name :") 3 age = input("input your age :") 4 job = input("input your job :") 5 salary = input("input your salary :") 6 7 info = ''' 8 ----------info of {0}-------------- 9 Name:{0}10 Age:{1}11 job:{2}12 salary:{3}13 '''.format(name,age,job,salary)14 print(info)
(4)
# Author:larllyname = input("input your name :")age = input("input your age :")job = input("input your job :")salary = input("input your salary :")info = """----------info of ''' + name +'''--------------Name:'''+ name +'''Age:'''+ age +'''job:'''+ job +'''salary:'''+ salary +""".format(name,name,age,job,salary)print(info)
2.字符创格式化输出
(1)# Author:larlly'''#打印字符串
print ("his name is %s " %("a")) #打印整数 print ("he is %d years old" %(25)) #打印浮点数 print ("his height is %f m" %(1.68)) #打印浮点数(指定保留小数点位数) print ("his height is %.2f m"%(1.69)) #指定占位符宽度 print ("name:%10s age:%8d height:%8.2f"%("abc",25,1.69)) #指定占位符宽度(左对齐) print("name:%-10s age:%-8d height:%-8.2f"%("abc",25,1.69)) #指定占位符(只能用0当占位符) print ("name:%-10s age:%08d height:%08.2f"%("abc",25,1.69)) #科学计数法 print(format(0.0015,'.2e'))
(2)字符串里面的%
是一个普通字符需要转义,用%%
来表示一个%
:
print("磁盘使用率:%d %%"% 7) 屏幕输出:如下 磁盘使用率:7 %