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

看一下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任何类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值