开发者

Not quite sure what the point of the %s is in Python, help?

开发者 https://www.devze.com 2023-02-05 17:23 出处:网络
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.

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?


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


The idea of %s in python is for formating.

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消