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 trialDennis Montegnies
1,112 PointsWhat's the difference between the enumerate function and just printing counter + item, the results are the same for me.
Let's say I have a list named weekdays with all the days of the week. What's the difference between following two lines of codes, as they have the same result?
print(f'{counter}. {day}')
print(str(counter) + ". " + day)
5 Answers
Eric M
11,546 PointsHi Dennis,
It's perhaps worth noting that neither of your examples rely on the enumerate function. Both are examples of printing strings. The first is using what's known as "Literal String Interpolation" which is a relatively new way of formatting strings that allows you to reference variables within the string itself. The second is using string concatenation, which joins strings together one after the other. You can see that counter has to be explicitly cast to a string here, because the +
operator can't perform the same operation on an integer and a string.
There are many ways to manipulate text in all programming languages. In the end a string is just an array (Python list) of characters, and characters are just bytes. It helps to have more abstract ways of dealing with them, and over time Python has gained many. You can look at the .append()
method, for instance, or using %s
for interpolation.
Cheers,
Eric
Darryl Mah
5,492 PointsYou’ll find that there can be a lot of redundancy in coding. Often you can have different methods to produce the same outcome. Sometimes the method you’ll choose will come down to a personal preference.
There is no difference between the two print statements you have up. However personally I like string interpolation, as I find it cleaner without needing to concat with +. Especially if there are multiple breaks in the string.
Dan B
6,155 PointsRead this, it should explain everything you need to know.
Important to mention, str.format() will be retired very soon. Time to get used to the brilliance of f-Strings!
Ryan Carson
23,287 PointsThank you for linking to that article. Really helpful!
Dennis Montegnies
1,112 PointsThanks, meanwhile I figured it out that none of my examples rely on the enumerate function.
I'm having some trouble to figure out the function of the f'
`part in my first print statement. Anyone can shine some light on this?
ERIN "Ernie" FOUGH
3,431 PointsSomeone gave a good answer in an answer to a question asked in an earlier module: it seems to be, essentially, a shorthand for the .format() method and I believe it was introduced (per the earlier answer) with 3.6.
- print(f'{counter}. {day}')
- print('{}. {}'.format(counter, day))
Jonathan Garza
4,475 PointsIt seems they both do the same thing in this example. However, the enumerate function is a built-in set of code for this purpose, meaning that it requires less effort to write when using it in the future for more complex datasets.