Which Python String Formatting to use?

Here are my go-to steps to decide which python string formatting to use.

Overview

There are different ways to format strings in Python. I quickly summarize the different methods here. You should dig deeper to see how these techniques can be used in your respective scenarios.

  1. Formatting with the % operator
  2. Formatting with .format() method
  3. Using f-strings
  4. Using Template strings

Method 1: Formatting with % operator

Here’s one of the oldest method among the four techniques described above. Included here only so we know it exists and when we come across it in older legacy or Python 2.6 code, we understand it is doing string formatting. It is also sometimes known as C-style formatting.

# Setups up variables we can use later in the example
>>> species = 'versicolor'
>>> petal_length = 13.498
# Example 1: Basic string substitution
>>> 'The species is %s' % species
# Outputs
The species is versicolor
---# Example 2: Basic float formatting with 2 decimal places accuracy
>>> 'The petal length is %.2f mm' % petal_length
# Outputs
The petal length is 13.50 mm
---# Example 3: Multiple positional arguments
>>> 'The %s petal averages at %.2f mm' % (species, petal_length)
# Outputs
The versicolor petal averages at 13.50 mm
---# Example 4: Named multi variables
>>> dictNames = {spec=species, length=petal_length}
>>> 'The %(spec)s petal averages at %(length).2f mm' % dictNames
# Outputs
The versicolor petal averages at 13.50 mm
  • It is sometimes more bug-prone when using this technique.
  • Whenever possible, avoid this and use more modern techniques.

Method 2: Formatting with .format() method

The .format() method exists to solve some of the quirks found in the earlier interpolation technique. Here are some examples of how to use the .format method.

# Setups up variables we can use later in the example
>>> species = 'versicolor'
>>> petal_length = 13.498
# Example 1: Basic string substitution
>>> 'The species is {}'.format(species)
# Outputs
The species is versicolor
---# Example 2: Basic float formatting with 2 decimal places accuracy
>>> 'The petal length is {:.2f} mm'.format(petal_length)
# Outputs
The petal length is 13.50 mm
---# Example 3: Multiple positional arguments
>>> 'The {} petal averages at {:.2f} mm'.format(species, petal_length)
# Outputs
The versicolor petal averages at 13.50 mm
---# Example 4: Named multi variables with kwargs
>>> 'The {spec} petal averages at {length:.2f} mm'.format(spec=species, length=petal_length)
# Outputs
The versicolor petal averages at 13.50 mm
  • Format specifiers are used to format strings, take a look at example 2, 3 and 4.
  • Named variables are referenced with kwargs sent in the format method.
  • Whenever possible, use this over interpolation method.
  • Format specifiers are useable in f-strings.

Method 3: Using f-strings

Released since Python 3.6. Preferable method over .format method when string is not generated by user. Prefixed with ‘f’ before the string and variables and expressions are evaluated within the curly braces.

# Setups up variables we can use later in the example
>>> species = 'versicolor'
>>> petal_length = 13.498
# Example 1: Basic string substitution
>>> f'The species is {species}
# Outputs
The species is versicolor
---# Example 2: Basic float formatting with 2 decimal places accuracy
>>> f'The petal length is {petal_length:.2f}'
# Outputs
The petal length is 13.50 mm
---# Example 3: With expressions
>>> f'The {species} petal averages at {petal_length + 3:.2f} mm'
# Outputs
The versicolor petal averages at 16.50 mm
---# Example 4: Named multi variables with kwargs
>>> f'The {species} petal averages at {petal_length:.2f} mm'
# Outputs
The versicolor petal averages at 13.50 mm
  • Only works with Python 3.6+.
  • Simple syntax by prefixing strings with ‘f’.
  • Expressions can be evaluated within curly braces.

Method 4: Template Strings

Template strings come with built-in Python and would be the preferred method of choice if the strings are user-generated.

The slight difference is the Template module must be imported when using the Template string format.

from string import Template# Setups up variables we can use later in the example
>>> species = 'versicolor'
>>> petal_length = 13.498
# Example 1: Basic string substitution
>>> t = Template('The species is $species')
>>> t.substitute(species=species)
# Outputs
The species is versicolor
---# Example 2: Basic float formatting with 2 decimal places accuracy
>>> t = Template('The petal length is $petal_length mm')
>>> t.substitute(petal_length=round(petal_length, 2))
# Outputs
The petal length is 13.50 mm
---# Example 3: Named multi variables with kwargs
>>> t = Template('The $species petal averages at $petal_length mm')
>>> t.substitute(species=species, petal_length=round(petal_length, 2))
# Outputs
The versicolor petal averages at 13.50 mm
  • Doesn’t allow format specifiers like the other techniques.
  • Slower than f-strings.
  • Slightly simpler syntax.
  • Preferably used with user-provided strings.

Conclusion

When choosing the formatting method, I preferably stick with the following questions.

  1. Is user-generated string? — Use Template Strings
  2. Is using Python 3.6+ and not user-generated? — Use f-strings
  3. Else — Use .format()

References

An avid web developer constantly looking for new web technologies to dabble in, more information can be found on bit.ly/jayden-chua

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store