python05 更多的变量和格式化打印

来源:互联网 发布:网络视频播放器 编辑:程序博客网 时间:2024/06/06 14:02

看一下C语言中的格式化打印

int x = 10;

printf("I am %d years old.",  x);

再看下python的示例

ex05.py

my_name = 'Zed A. Shaw'my_age = 35my_height = 74my_weight = 180my_eyes = 'Blue'my_teeth = 'White'my_hair = 'Brown'print "Let's talk about %s." % my_nameprint "He's %d inches tall." % my_heightprint "He's %d pounds heavy." % my_weightprint "Actually that's not too heavy."print "He's got %s eyes and %s hair." % (my_eyes, my_hair)print "His teeth are usually %s depending on the coffee." % my_teeth# this line is tricky, try to get it exactly rightprint "If I add %d, %d, and %d I get %d." % (    my_age, my_height, my_weight, my_age + my_height + my_weight)print "%(name)s: %(score)06.2f" % {'score':9.5, 'name':'newsim'}print "%(name)s: %(score)6.2f" % {'score':9.5, 'name':'newsim'} print "%(name)r: %(score)r" % {'score':9.5, 'name':'newsim'}

注意:如果你使用了非 ASCII 字符而且碰到了编码错误,记得在最顶端加一行#--coding:utf-8--

字符串格式化代码
格式描述%%百分号标记%c字符及其ASCII码%s字符串%d有符号整数(十进制)%u无符号整数(十进制)%o无符号整数(八进制)%x无符号整数(十六进制)%X无符号整数(十六进制大写字符)%e浮点数字(科学计数法)%E浮点数字(科学计数法,用E代替e)%f浮点数字(用小数点符号)%g浮点数字(根据值的大小采用%e或%f)%G浮点数字(类似于%g)%p指针(用十六进制打印值的内存地址)%n存储输出字符的数量放进参数列表的下一个变量中%r任何类型

原创粉丝点击