Python Tutorials · Python String Formatting

Python String Formatting

Learn all about Python String Formatting in this comprehensive tutorial.

5 min read intermediate
  • F-String was introduced in Python 3.
  • F-string allows you to format selected parts of a string.
  • To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value.
  • You can perform Python operations inside the placeholders.
  • You can execute functions inside the placeholder:
  • At the beginning of this chapter we explained how to use the .
  • Before Python 3.
  • If you want to use more values, just add more values to the format() method:
  • You can use index numbers (a number inside the curly brackets {0}) to be sure the values are placed in the correct placeholders:
  • You can also use named indexes by entering a name inside the curly brackets {carname}, but then you must use names when you pass the parameter values txt.

Introduction

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

Before Python 3.6 we had to use the format() method.

F-Strings

F-string allows you to format selected parts of a string.

To specify a string as an f-string, simply put an f in front of the string literal, like this:

python

Placeholders and Modifiers

To format values in an f-string, add placeholders {}, a placeholder can contain variables, operations, functions, and modifiers to format the value.

python

A placeholder can also 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

You can also format a value directly without keeping it in a variable:

python

Perform Operations in F-Strings

You can perform Python operations inside the placeholders.

You can do math operations:

python

You can perform math operations on variables:

python

You can perform if...else statements inside the placeholders:

python

Execute Functions in F-Strings

You can execute functions inside the placeholder:

python

The function does not have to be a built-in Python method, you can create your own functions and use them:

python

More Modifiers

At the beginning of this chapter we explained how to use the .2f modifier to format a number into a fixed point number with 2 decimals.

There are several other modifiers that can be used to format values:

python

Here is a list of all the formatting types.

Formatting Types
:<Try itLeft aligns the result (within the available space)
:>Try itRight aligns the result (within the available space)
:^Try itCenter aligns the result (within the available space)
:=Try itPlaces the sign to the left most position
:+Try itUse a plus sign to indicate if the result is positive or negative
:-Try itUse a minus sign for negative values only
:Try itUse a space to insert an extra space before positive numbers (and a minus sign before negative numbers)
:,Try itUse a comma as a thousand separator
:_Try itUse a underscore as a thousand separator
:bTry itBinary format
:cConverts the value into the corresponding Unicode character
:dTry itDecimal format
:eTry itScientific format, with a lower case e
:ETry itScientific format, with an upper case E
:fTry itFix point number format
:FTry itFix point number format, in uppercase format (show inf and nan as INF and NAN)
:gGeneral format
:GGeneral format (using a upper case E for scientific notations)
:oTry itOctal format
:xTry itHex format, lower case
:XTry itHex format, upper case
:nNumber format
:%Try itPercentage format

String format()

Before Python 3.6 we used the format() method to format strings.

The format() method can still be used, but f-strings are faster and the preferred way to format strings.

The next examples in this page demonstrates how to format strings with the format() method.

The format() method also uses curly brackets as placeholders {}, but the syntax is slightly different:

python

You can add parameters inside the curly brackets to specify how to convert the value:

python

Check out all formatting types in our String format() Reference.

Multiple Values

If you want to use more values, just add more values to the format() method:

python

And add more placeholders:

python

Index Numbers

You can use index numbers (a number inside the curly brackets {0}) to be sure the values are placed in the correct placeholders:

python

Also, if you want to refer to the same value more than once, use the index number:

python

Named Indexes

You can also use named indexes by entering a name inside the curly brackets {carname}, but then you must use names when you pass the parameter values txt.format(carname = "Ford"):

python

Module quiz

2 questions
1

Which of the following is true about Python String Formatting?

2

What is the most common pitfall when working with Python String Formatting?

Answer all questions to submit.