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
Luke Hayes
Python Web Development Techdegree Student 1,230 PointsFormatting strings, preference or is one better than the other?
Hello,
Before I signed up for the Techdegree here on Treehouse I was already learning Python and the way that I was formatting strings looked like this name = "Bob" print(f"Hello, {name}")
The way here on Treehouse is the .format() way. Is there any advantage to this? This question may seem trivial but I just found the original way that I learned more intuitive and easier.
Thanks.
1 Answer
andren
28,558 PointsThere are actually about 4 ways of doing string interpolation in Python:
- %-Formatting
- str.format() (the way Treehouse teaches)
- Template-String
- F-String (this is the way you were originally taught)
Which technique you want to use is mostly a matter of preference, but one thing to be aware of is that F-Strings are a pretty recent addition to the Python Language. It was introduced in Python 3.6 which I'm pretty sure came out after most of the Treehouse courses was recorded.
Treehouse also appears to be running Python 3.5 in their console and for the code challenges so F-Strings are not really usable within Treehouse. As for outside Treehouse you can use them but be aware that you are then limiting your code to only run on Python 3.6 and up.