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

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.

Avatar image for philraffsr

philraffsr on April 11, 2019

Simple and straightforward. Would like to have seen some decimal/left-right justification/zero-fill examples.

Avatar image for Wiggers

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!

Avatar image for Eriberto

Eriberto on April 12, 2019

Formatting using the f-string method is the one i preferred the most.

Avatar image for Pavel Zaikin

Pavel Zaikin on April 14, 2019

A lot of examples: pyformat.info

Avatar image for Malef

Malef on April 14, 2019

Thank for straightforward and informative way of presenting information.

Avatar image for adriaprat

adriaprat on April 15, 2019

As philraffsr said, some decimal/left-right justification/zero-fill examples would be nice. 😉

Avatar image for gracebr28

gracebr28 on June 4, 2019

Thanks for the great tutorial!

Avatar image for Abby Jones

Abby Jones on June 27, 2019

Very informative!

Avatar image for aradim

aradim on July 14, 2019

Very nice! Thank you.

Avatar image for Pakorn

Pakorn on Dec. 17, 2019

great tutorial

Avatar image for alvesmig

alvesmig on June 28, 2020

Thank you for this great tutorial

Avatar image for dannysharkey80

dannysharkey80 on Aug. 13, 2020

What is meant by a “user supplied format string”? I’m not sure understand the use-case.

Avatar image for Bartosz Zaczyński

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.

Avatar image for Yvonne Wilmot

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.