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

.format() VS "", x, ""

Hello,

I see this method used in this video:

name = "Michael" print(Great job {}, keep it up!".format(name))

But I was using this method:

name = "Michael" print("Great job", name, ", keep it up!")

I'm curious if one method is better than the other or more commonly used?

1 Answer

Rick Gleitz
Rick Gleitz
47,876 Points

Hi Michael, You can use either one, especially for a short statement. But if you are using a person's name multiple places in a longer string (like a form letter), the format method is way simpler. It's good to know both. Hope this helps.

Thanks for the response Rick. Ohhh gotcha. Yeah that would make way more sense if you were to repeat that many times. In these short use cases I guess the way I was doing it is more efficient. But now I see how the other way would be more beneficial in a lot of cases. Thanks for helping me understand.