在虫师的博客园中看到以下代码,对其中自己还不太熟悉的知识点记个笔记
>>> name = 'huhu'
>>> age = 26
>>> print "my name is %s" %name
my name is huhu
>>> print "my age is %d" %age
my age is 26
>>> print "my name is %d" %name
Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> print "my name is %d" %name TypeError: %d format: a number is required, not str
>>> print "my name is %s ,age is %d" %(name,age)
my name is huhu ,age is 26
在print中打印字符串的时候引用了两个变量,需要用到“格式化字符串”,其中“%s”表示输出的类型为字符串,“%d”表示输出类型为整型数字。如果不确定变量类型的话可以使用“%r”,含义是“不管什么都打印出来”