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
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
Related Q & A:
- What to do for Valentine's Day,what do guys like?Best solution by Yahoo! Answers
- My husband and I have a major career decision to make and we're just not sure what to do?Best solution by Yahoo! Answers
- What is a CVT transmission and how does it help me?Best solution by Yahoo! Answers
- Not sure what dirt bike boots to get?Best solution by motosport.com
- Advice on what to do to become a police officer? please help?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.