文中Python源码中%10的作用是什么,,x = "There a


x = "There are %d types of people." % 10

这个叫格式化字符串。

>>> uid = "sa">>> pwd = "secret">>> print pwd + " is not a good password for " + uidsecret is not a good password for sa>>> print "%s is not a good password for %s" % (pwd, uid) secret is not a good password for sa>>> userCount = 6>>> print "Users connected: %d" % (userCount, )            Users connected: 6>>> print "Users connected: " + userCount                 Traceback (innermost last):  File "<interactive input>", line 1, in ?TypeError: cannot concatenate 'str' and 'int' objects

更多可以看 Dive Into Python - 格式化字符串

其他语言的printf

>>>x = "a %d c" % 10  >>>print xa 10 c

编橙之家文章,

评论关闭