看一下C语言中的格式化打印
int x = 10;
printf("I am %d years old.", x);
再看下python的示例
ex05.py
my_name = 'Zed A. Shaw'
my_age = 35
my_height = 74
my_weight = 180
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "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 right
print "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 | 任何类型 |