%d和%s的目的是什么?
Before you get mad at me for asking this question, I have looked for the answer but still can't seem to get what exactly they do. I saw this one answer by a user named "marcog" on SOF. For example :
在你因为提出这个问题而生我的生气之前,我已经找到了答案,但似乎仍无法得到他们到底做了什么。我在SOF上看到了一个名为“marcog”的用户的回答。例如 :
name = "marcog"
number = 42
print "%s %d" (name, number)
What I did then was deleting this part and the code still outputs the exact same thing.
我所做的就是删除这部分,代码仍然输出完全相同的东西。
name = "marcog"
number = 42
print(name, number)
Could you please briefly explain the reason we use them ? By the way, I'm using 3.3.1.
你能简单解释一下我们使用它们的原因吗?顺便说一下,我正在使用3.3.1。
3 个解决方案
#1
12
It tells print
to output a string (%s
) and a signed integer (%d
). This is more useful if you want to have a more complicated output. For example:
它告诉print输出一个字符串(%s)和一个有符号整数(%d)。如果您想要更复杂的输出,这将更有用。例如:
print("Hello %s your number is %d" % (name, number))
With the sample values you've given, this will output:
使用您给出的样本值,这将输出:
Hello marcog your number is 42
你好marcog你的号码是42
更多相关文章
- 如何在python中使用命名组和datadict从正则表达式模式中组合字符
- 字符串压缩 牛客网 程序员面试金典 C++ Python
- Python 用hashlib求中文字符串的MD5值
- 将字节列表转换为字节字符串
- [D]用python提取多段字符串该怎么写正则表达式。
- 如何用位于括号外的逗号分隔字符串?
- 用于搜索和替换大字符串的最快Python方法
- python 字符串操作
- 为什么Python的eval()拒绝这个多行字符串,我如何修复它?