不太确定 %s 在 Python 中的意义是什么,帮忙吗?
我现在正在从一本书上学习Python,我不明白使用 %s 在列表、字符串、字典等中定位特定项目的意义是什么。
例如:
names = ["jones", "cohen", "smith", "griffin"]
print(names[1])
print("%s" % names[1])
两个命令都打印“cohen, “使用 %s 有什么意义?
I'm learning Python from a book right now and I can't figure out what the point is of using the %s to site a specific item in a list, string, dictionary, etc.
For example:
names = ["jones", "cohen", "smith", "griffin"]
print(names[1])
print("%s" % names[1])
Both commands print "cohen," what's the point of ever using the %s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个想法是让你轻松创建更复杂的输出,例如
,而不是
但是,由于你刚刚开始使用Python,你应该开始学习 新的字符串格式化语法 马上:
当然,这个例子并不能展示字符串格式化方法的真正威力。您可以用这些做更多的事情,例如(取自上面链接的文档):
等等......
The idea is to allow you to easily create more complicated output like
instead of
However, as you're just starting to use Python, you should start learning the new string formatting syntax right away:
Of course, this example can't show the real power of string formatting methods. You can do much more with those, for example (taken from the docs linked above):
and so on...
python 中 %s 的想法是用于格式化。
The idea of %s in python is for formating.
%s
用于构造字符串。与许多其他语言一样,Python 中的字符串是不可变的。因此,如果连接很多字符串,每个字符串都会被创建并存储在内存中等待垃圾收集。
因此,
%s
的要点是,如果您必须连接许多不同的字符串,请构造一次字符串,从而节省不必要的内存开销。它也可以说是一种比
+
更方便的语法,并且可以在需要的地方中断字符串。%s
is used to construct a string.In python, like in many other languages, strings are immutable. So, if you concatenate a lot of strings, each of them is created and stored in the memory waiting to be garbage collected.
The point of
%s
, so, is, if you have to join many different strings, construct the string once and hence save unnecessary memory overhead.It is also arguably a much more convenient syntax than the
+
and breaking strings where need to be.print(names[1]) 仅打印 str() 表示形式
另一方面, print("%s" % names[1]) 打印格式字符串“%s”,其中填充了名称[1],
这里的效果是相同的。
使用 print(n1, n2, n3) 可以打印多个由空格分隔的数据对象。将其视为硬编码。
使用 print(" some format string " % (n1, n2, n3)) 您可以“美化”您的输出。格式字符串可以是您组合在一起的变量,因此可以在代码运行时更改。
print(names[1]) just prints the str() representation
print("%s" % names[1]) on the other hand prints the format string "%s" which is filled with names[1]
the effect here is the same.
with print(n1, n2, n3) you can print several data objects separated by a space. think of it as hard coded.
with print(" some format string " % (n1, n2, n3)) you can "beautify" your output. the format string could be a variable that you put together so this could change during runtime of the code.
使用
%s
只是使用我所说的printf
格式。它与 C 等编程语言很相似。正如 Tim 所指出的,Python 有一种新的首选方式来格式化字符串,您可能应该学习它。但老方法仍然很强大。尝试 man sprintf 看看如何指定标志、字段宽度、精度等。我认为 python 的 print 与所有这些兼容。Using
%s
is just using what I would callprintf
format. It's familiar from programming languages like C. As pointed out by Tim, python has a new preferred way to format strings which you should probably learn. But the old way is still pretty powerful. Tryman sprintf
to see how you can specify flags, field width, precision, etc. I think python's print is compatible with all that.