1. 脚本中写中文出现乱码,用以下可以解决。参考http://www.python.org/dev/peps/pep-0263/
# coding=<encoding name> #!/usr/bin/python # -*- coding: <encoding name> -*- #!/usr/bin/python # vim: set fileencoding=<encoding name> :
2. 输出格式,先看代码
my_name = 'Zed A. Shaw' print "Let's talk about %s." % my_name print "========================================================" print round(2.00034, 3) #2.0 print round(345.2345, 2) #345.23 print "hex of 1444 is %x" % 1444 #5a4
round(num, n)函数是对浮点数num对第n位小数四舍五入,n取正负均可。
round(345, -1) = 350.0
round(345, -3) = 0.0
round(345, 0) = 345.0
以下是对Python format characters的总结,摘自http://docs.python.org/2/library/stdtypes.html#string-formatting-operations