【习题5】更多的变量和打印【第6天】
【代码】
1 #coding:utf-8 2 my_name='liujianqi' 3 my_age=25 # not a lie 4 my_height=172 # cm 5 my_weight=100 #kg 6 my_eyes='black' 7 my_teeth='white' 8 my_hair='black' 9 10 print "Let's talk about %s." % my_name 11 print "He's %d cm tall." %my_height 12 print "He's %d kg weight" %my_weight 13 print "Actually that's too heavy" 14 print "He's got %s eyes and %s hair." %(my_eyes,my_hair) 15 print "His teeth are usually %s depending on the coffee." %my_teeth 16 17 # this line is tricky,try to get it exactly right 18 print "If I add %d , %d,and %d I get %d"%(my_age,my_height,my_weight,my_age+my_height+my_weight)
【运行结果】:^_^
【补充:Python格式化字符】
%s 字符串 (采用str()的显示)
%r 字符串 (采用repr()的显示)
%c 单个字符
%b 二进制整数
%d 十进制整数
%i 十进制整数
%o 八进制整数
%x 十六进制整数
%e 指数 (基底写为e)
%E 指数 (基底写为E)
%f 浮点数
%F 浮点数,与上相同
%g 指数(e)或浮点数 (根据显示长度)
%G 指数(E)或浮点数 (根据显示长度)
%% 字符"%"