不太确定 %s 在 Python 中的意义是什么,帮忙吗?

发布于 2024-10-13 07:35:26 字数 223 浏览 4 评论 0原文

我现在正在从一本书上学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾`听者〃 2024-10-20 07:35:26

这个想法是让你轻松创建更复杂的输出,例如

print("The name is %s!" % names[1])

,而不是

print("The name is " + names[1] + "!")

但是,由于你刚刚开始使用Python,你应该开始学习 新的字符串格式化语法 马上:

print("The name is {}!".format(names[1])

当然,这个例子并不能展示字符串格式化方法的真正威力。您可以用这些做更多的事情,例如(取自上面链接的文档):

>>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated
'abracadabra'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
>>> coord = (3, 5)
>>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
'X: 3;  Y: 5'
>>> # format also supports binary numbers
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

等等......

The idea is to allow you to easily create more complicated output like

print("The name is %s!" % names[1])

instead of

print("The name is " + names[1] + "!")

However, as you're just starting to use Python, you should start learning the new string formatting syntax right away:

print("The name is {}!".format(names[1])

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):

>>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated
'abracadabra'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'
>>> coord = (3, 5)
>>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
'X: 3;  Y: 5'
>>> # format also supports binary numbers
>>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42)
'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010'

and so on...

佞臣 2024-10-20 07:35:26

python 中 %s 的想法是用于格式化。

a = 1.23
print "The value is %0.5f" %(a) # prints 1.23000

The idea of %s in python is for formating.

a = 1.23
print "The value is %0.5f" %(a) # prints 1.23000
青柠芒果 2024-10-20 07:35:26

%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.

花开半夏魅人心 2024-10-20 07:35:26

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.

转瞬即逝 2024-10-20 07:35:26

使用 %s 只是使用我所说的 printf 格式。它与 C 等编程语言很相似。正如 Tim 所指出的,Python 有一种新的首选方式来格式化字符串,您可能应该学习它。但老方法仍然很强大。尝试 man sprintf 看看如何指定标志、字段宽度、精度等。我认为 python 的 print 与所有这些兼容。

Using %s is just using what I would call printf 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. Try man sprintf to see how you can specify flags, field width, precision, etc. I think python's print is compatible with all that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文