Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Formatting Python Strings
In the previous tutorial in this introductory series, you got a first look at how you can format your strings with Python f-strings. You’ll learn more about this technique towards the end of this tutorial.
Since f-strings are comparably new to the Python language, it’s good for you to be familiar with a second, slightly older technique as well. You’re likely to encounter it in older Python code.
In this tutorial, you’ll learn about:
- The string
.format()method - The formatted string literal, or f-string
You’ll learn about these formatting techniques in detail and add them to your Python string formatting toolkit.
Note: There’s a standard module called string containing a class called Template, which also provides some string formatting through interpolation. A related technique, the string modulo operator, provides more or less the same functionality. You won’t cover either of these techniques in this tutorial, but you’ll want to read up on the string modulo operator if you have to work with code written in Python 2.
You might notice that there are a lot of different ways to format strings in Python, which breaks with one of Python’s very own mantras:
There should be one– and preferably only one –obvious way to do it. —Tim Peters, The Zen of Python
The different string formatting techniques have developed historically over the years that Python has existed and grown. Older techniques are kept around to ensure backward compatibility, and some even have specific use cases.
If you’re getting started with Python and you’re looking for a single way to format your strings, then stick with Python’s f-strings. If you’ve encountered strange-looking string format code and want to know more, or you need to work with older versions of Python, then it’s a good idea to learn about the other techniques as well.
The Python String .format() Method
The Python string .format() method was introduced in version 2.6. It’s similar in many ways to the string modulo operator, but .format() goes well beyond in versatility. The general form of a Python .format() call is shown below:
<template>.format(<positional_argument(s)>, <keyword_argument(s)>)
Note that this is a method, not an operator. You call the method on <template>, which is a string containing replacement fields. The <positional_arguments> and <keyword_arguments> to the method specify values that are inserted into <template> in place of the replacement fields. The resulting formatted string is the method’s return value.
In the <template> string, replacement fields are enclosed in curly braces ({}). Anything not contained in curly braces is literal text that’s copied directly from the template to the output. If you need to include a literal curly bracket character, like { or }, in the template string, then you can escape this character by doubling it:
>>> '{{ {0} }}'.format('foo')
'{ foo }'
Now the curly braces are included in your output.
The String .format() Method: Arguments
Let’s start with a quick example to get you acquainted before you dive into more detail on how to use this method in Python to format strings. Using the older formatting technique with the string modulo operator, the code would be a little hard to read:
>>> print('%d %s cost $%.2f' % (6, 'bananas', 1.74))
6 bananas cost $1.74
In the example above, you used the string modulo operator to format the string. For better readability, you can use Python’s string .format() method to obtain the same result:
>>> print('{0} {1} cost ${2}'.format(6, 'bananas', 1.74))
6 bananas cost $1.74
In this example, <template> is the string '{0} {1} cost ${2}'. The replacement fields are {0}, {1}, and {2}, which contain numbers that correspond to the zero-based positional arguments 6, 'bananas', and 1.74. Each positional argument is inserted into the template in place of its corresponding replacement field:

The next example uses keyword arguments instead of positional parameters to produce the same result:
>>> print('{quantity} {item} cost ${price}'.format(
... quantity=6,
... item='bananas',
... price=1.74))
6 bananas cost $1.74
In this case, the replacement fields are {quantity}, {item}, and {price}. These fields specify keywords that correspond to the keyword arguments quantity=6, item='bananas', and price=1.74. Each keyword value is inserted into the template in place of its corresponding replacement field:

You’ll learn more about positional and keywords arguments there in the next tutorial in this introductory series, which explores functions and argument passing. For now, the two sections that follow will show you how these are used with the Python .format() method.
Positional Arguments
Positional arguments are inserted into the template in place of numbered replacement fields. Like list indexing, the numbering of replacement fields is zero-based. The first positional argument is numbered 0, the second is numbered 1, and so on:
>>> '{0}/{1}/{2}'.format('foo', 'bar', 'baz')
'foo/bar/baz'