What is the point of a minor?

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

  • 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?

  • Answer:

    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 http://docs.python.org/library/string.html#new-string-formatting 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...

Zack Shapiro at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The idea of %s in python is for formating. a = 1.23 print "The value is %0.5f" %(a) # prints 1.23000

Sai Venkat

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

Lakshman Prasad

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.

lightning2911

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.

pythonic metaphor

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.