Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

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.

Download

Sample Code (.zip)

2.1 KB
Download

Course Slides (.pdf)

293.5 KB
Avatar image for Mark Walle

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.

Avatar image for Ghani

Ghani on Nov. 29, 2020

Thanks for this well-structured and useful course.

Avatar image for jamesbrown68

jamesbrown68 on Nov. 30, 2020

Thanks, Liam.

Avatar image for thecocoa

thecocoa on Dec. 8, 2020

Thank you, Liam. Very informative.

Avatar image for jwimmer

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.