Python Tutorials · Python Strings

Format Strings

Learn all about Format Strings in this comprehensive tutorial.

5 min read beginner
  • As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:
  • F-String was introduced in Python 3.
  • A placeholder can contain variables, operations, functions, and modifiers to format the value.

String Format

As we learned in the Python Variables chapter, we cannot combine strings and numbers like this:

python

But we can combine strings and numbers by using f-strings or the format() method!

F-Strings

F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.

To specify a string as an f-string, simply put an f in front of the string literal, and add curly brackets {} as placeholders for variables and other operations.

python

Placeholders and Modifiers

A placeholder can contain variables, operations, functions, and modifiers to format the value.

python

A placeholder can include a modifier to format the value.

A modifier is included by adding a colon : followed by a legal formatting type, like .2f which means fixed point number with 2 decimals:

python

A placeholder can contain Python code, like math operations:

python

Learn more about String Formatting in our String Formatting chapter.

Module quiz

2 questions
1

Which of the following is true about Format Strings?

2

What is the most common pitfall when working with Format Strings?

Answer all questions to submit.