Formatting Python Strings (Summary)
In this course, you mastered two additional techniques that you can use in Python to format string data. You should now have all the tools you need to prepare string data for output or display!
You might be wondering which Python formatting technique you should use. Under what circumstances would you choose .format()
over the f-string? See Python String Formatting Best Practices for some considerations to take into account.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
Over the course of this lesson, you’ve become a master of formatting strings. You learned about the string .format()
method and the <name>
, !<conversion>
, :<format_spec>
syntax, as well as all of the sub-specifications of the <format_spec>
.
00:15
And you learned how the formatted string literal can be used as kind of a shorthand for the string .format()
method and provides a lot of the same features with a much more concise syntax. I hope you enjoyed the course!
00:26 I had a lot of fun making it. And now, go out and use these awesome string formatting techniques in your own work. Thanks again.
Ghani on Nov. 29, 2020
Thanks for this well-structured and useful course.
jamesbrown68 on Nov. 30, 2020
Thanks, Liam.
thecocoa on Dec. 8, 2020
Thank you, Liam. Very informative.
jwimmer on April 13, 2022
Awesome veeery handy :) Especially after I tried really hard to find a solution to how to loop through dictionaries and format the output in a tidy way. As a beginner who aspires to become a game developer I discovered my many basic Python knowledge gaps and work now through Real Pythons Basic, which is a fun and way less distressing way than my previous approach of panicking through book chapters which where too demanding because I was too demanding and didn’t know the solution. I will come back to them at a later point. The video tutorials are also very convenient. They show complex concepts and explain the solution in an easy disgestable / understandable way.
Become a Member to join the conversation.
Mark Walle on Nov. 24, 2020
We learned about
.format()
before f-String literals because you wanted us to understand the precursor function before learning the syntactic sugar version of it.In the same way, I would like to see a tutorial showing string interpolation using the printf-style (that uses the ‘
%
’ interpolation operator) (docs.python.org/3/library/stdtypes.html#old-string-formatting), which is more like a “C style” string interpolator (which would make it the ultimate “under the hood” approach to borrow your phrase).I would also argue that “%” style interpolation is still relevant: it helps us to understand why the
.format()
function is so handy to have in the first place; and it is also remains relevant in terms of its use for the python logger module to format messages using late-evalution for it’s performance advantage.Please consider a tutorial on it as part of a larger course on strings.