1.格式化输出整数python print也支持参数格式化,与C言的printf似, strHello = "the length of (%s) is %d" %(Hello World,len(Hello World))print strHello#输出果:the length of (Hello World) is 11 2.格式化输出16制整数nHex = 0x20#%x --- hex 十六进制#%d --- dec 十进制#%d --- oct 八进制 print &quo
Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sprintf 函数一样的语法. 如下实例: #!/usr/bin/python print "My name is %s and weight is %d kg!" % ('Zara', 21) 以上实例输出结果: My name is Zara and weight is 21 kg! p