玩蛇网提供最新Python编程技术信息以及Python资源下载!

文中Python源码中%10的作用是什么

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

这个叫格式化字符串。

>>> uid = "sa"
>>> pwd = "secret"
>>> print pwd + " is not a good password for " + uid
secret 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 x
a 10 c

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd20166.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月19日 - 13时35分00秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules