Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Objects Create a Class with a Method

David Bouchare
David Bouchare
9,224 Points

[code challenge] %s on a method

Hi everyone,

I get an error message saying I don't get the expected output while writing the following:

class Store:
    open = 9
    close = 18

def hours(self):
    return "We're open from %s to %d" % (self.open, self.close)

Yet it does return the correct values: "We're open from 9 to 18.". I know we have to use the .format() syntax (which I don't really know how to use) but not sure if the %s should work as well.

Thanks,

David

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

So %s puts in a string and %d puts in a digit. Why use both?

David Bouchare
David Bouchare
9,224 Points

Fair point, I didn't even know that. Should have used %d on both cases. Thanks.

2 Answers

I think the %s only works in python 2. To use the .format() syntax you would do this

  def hours(self):
    return "We're open from {} to {}.".format(self.open,self.close)

You just use the curly braces where you would have used %s and use .format() after the closing parentheses. If you want you can also include the index of the item in the curly braces. So this also works in case you didn't want to list the items in order.

  def hours(self):
    return "We're open from {1} to {0}.".format(self.close, self.open)

Hope that helps.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

%s works fine in Python 3 (it's just ugly). David Bouchare's problem was that he forgot the period. ;)

David Bouchare
David Bouchare
9,224 Points

Thanks Brittany, works totally fine now!

David Bouchare
David Bouchare
9,224 Points

Ah ah... Yes didn't notice that either! Why is it ugly?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Personal opinion. I find .format() to be much more explicit and "beautiful".

Hannah Taylor
Hannah Taylor
2,407 Points

So is there any difference in how the two of these function e.g. in terms of how efficiently the code runs, or one of them being more pythonic? I leaned %s, %d etc. but have seen the {} .format() throughout these courses.

I teach high school Computer Science, but have a background in web so I'm still learning Python, and since I'm teaching would like to aim for best practices. Is there any difference other than personal preference? Thanks :)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hannah Taylor AFAIK, there's no performance difference between the two, but I haven't done any benchmarks, etc. I remember that, at some point, there was talk of removing %s etc from Python 3 and .format() seems to be more heavily encouraged, but I think that's about it.