Python String Formatting: Recap & Summary
Summary of what you’ve learned in this course:
- String Format with
%
- String Format with
.format()
- f-strings
- Template Strings
Knowing which method to use to format strings can be confusing, In this lesson you’ll see a handy flowchart to help you decide which formatting method to use.
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:01
In this series, we’ve covered the four major ways of string formatting. The first was string formatting with the interpolation operator (%
), the second string formatting with the .format()
method, the third was f-strings, and the fourth was template strings.
00:18
The thing that these four major ways of string formatting all have in common is that you have to indicate where in the string you’re going to substitute your variables. With the interpolation operator, it’s the interpolation operator. With .format()
, it’s curly brackets. With f-strings, it’s curly brackets containing a keyword.
00:40
And with template strings, it’s a dollar sign ($
) followed by a keyword. If you’re like me, when you first learned all these it was tough to remember which one to use and when, so I’ve got a quick flow chart to show you.
00:55 Starting at the top, are there user-supplied format strings? If yes, consider using template strings. If not, are you on Python 3.6? If yes, consider using f-strings.
01:09
If not, use the .format()
method. Give yourself a pat on the back, you did it! You got through all four major ways of string formatting and best practices!
Wiggers on April 11, 2019
Why in books does it do this print('Hey ' ;name; 'There is an error '; string(errno)
? It confused me the first time some one put f strings in my code!
Eriberto on April 12, 2019
Formatting using the f-string method is the one i preferred the most.
Pavel Zaikin on April 14, 2019
A lot of examples: pyformat.info
Malef on April 14, 2019
Thank for straightforward and informative way of presenting information.
adriaprat on April 15, 2019
As philraffsr said, some decimal/left-right justification/zero-fill examples would be nice. 😉
gracebr28 on June 4, 2019
Thanks for the great tutorial!
Abby Jones on June 27, 2019
Very informative!
aradim on July 14, 2019
Very nice! Thank you.
Pakorn on Dec. 17, 2019
great tutorial
alvesmig on June 28, 2020
Thank you for this great tutorial
dannysharkey80 on Aug. 13, 2020
What is meant by a “user supplied format string”? I’m not sure understand the use-case.
Bartosz Zaczyński RP Team on Aug. 14, 2020
User-supplied strings would be anything that comes from your application’s users rather than you as the author. It could be a comment left on a website or something typed on the keyboard in a command-line app.
For security reasons, you should always assume that such external content may potentially contain malicious data that requires special treatment. Using a template string is safer than all the other methods because it has some countermeasures to prevent the attacks.
Yvonne Wilmot on Sept. 23, 2020
Thanks a lot. That clarified where the different ways of string formatting fit in and when/where each method is supposed to be used.
Become a Member to join the conversation.
philraffsr on April 11, 2019
Simple and straightforward. Would like to have seen some decimal/left-right justification/zero-fill examples.