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 trialMike Atkinson
6,882 PointsDiscussion: "{}".format(var) compared to "%s" % (str(var))
Differences between
var = "Some String"
print("{}".format(var))
and
var = "Some String"
print("%s" % (var))
1 Answer
Charlie L.
10,120 PointsI believe that the % syntax is older and less favored in python 3.3+. It requires you do specify what the value is through different notation (%s for str, %d for num, etc). The .format() seems to be more reliable in catering to all types of input.
In the end it may be preference to use one over the other but the .format() does seem to provide more usability.
I linked a post in stackoverflow that asked the same question: http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format
Mike Atkinson
6,882 PointsMike Atkinson
6,882 PointsThanks for the reply Charlie and thanks for the link, I'll check it out.