1. 一般格式化输出记得加括号, 且无逗号 print "%d" % (variable)


2. print "hello" 自带\n

3. print "hello", 加上逗号之后,不会换行,但会加一个空格

4. 如果print加逗号不换行的话会有一个空格 而sys.stdout.write()就没有;


5. %r是一个万能的格式付,它会将后面给的参数原样打印出来,带有类型信息。

python print %r 案例


formatter = "%r %r %r %r"
 
print formatter % (1, 2, 3, 4)
print formatter % ("one", "two", "three", "four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
 "But it didn't sing.",
 "So I said goodnight."
 )