python的输出
规则
字符串的格式化符号:
%f 小数, 浮点数
%.2f 保留两位小数点的浮点数
%d 整形数
%s 字符串
%o 八进制
%x 十六进制
范例
"%.3d" %(1)
Out[8]: '001'
"%10.3d" %(1)
Out[9]: ' 001'
"%.2f%%" %(mem_percent)
Out[15]: '30.00%'
name = "python"
print "hello %s" %(name)
sid = 1
print "hello %.3d" %(sid)
python2:
- raw_input(): 接收字符串的数据;
- input(): 只能接收数值;类型;
name = raw_input("input name:")
print name
info = input("input:")
print info
拓展练习
# 字符串输出
print "%s" %(001)
1
# 整形数输出
print "%d" %(001)
1
# 八进制转化为十进制输出
print "%d" %(0011)
9
# 整形转化为八进制输出
print "%o" %(9)
11
print "%o" %(10)
12
print "%o" %(11)
13
# 整形转化为十六进制输出
print "%x" %(16)
10
print "%x" %(17)
11
print "%x" %(11)
b
# 浮点数输出
print "%f" %(11)
11.000000
# 浮点数保留位数输出
print "%.3f" %(11)
11.000
print "%.2f" %(11)
11.00
print "内存占有率:%.2f%" %(1.23324546456)
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2882, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-21-5446e13955af>", line 1, in <module>
print "内存占有率:%.2f%" %(1.23324546456)
ValueError: incomplete format
print "内存占有率:%.2f%%" %(1.23324546456)
内存占有率:1.23%
print "内存占有率:%10.2f%%" %(1.23324546456)
内存占有率: 1.23%
print "内存占有率:%-10.2f%%" %(1.23324546456)
内存占有率:1.23 %