How to use Python string format
Python provides excellent support for string operations and text processing. The language has comprehensive methods for formatting and outputting text. We’ll give you an overview of the most important methods for Python string formatting.
What are the string format methods in Python used for?
Strings are sequences of characters, and they can contain single letters, words, sentences or entire documents in Python. Methods are needed to format strings.
Strings can consist of several pieces. In many cases, the static string components and variable data should be mixed. This is required when outputting data and creating documents in web development.
Let’s use an online store as an example. We want to calculate the total price of the products in a user’s shopping cart and show it to them using the following message: “The total price is […] dollars.” The first step would be to assemble the string using the concatenation operator:
price = 69.42
message = "The total price is" + price + "USD".
PythonAn error message is displayed because the price is a number and cannot be concatenated with a string. We have to convert the number to a string using the str() function first:
message = "The total price is" + str(price) + "USD".
PythonThis can get confusing, so it’s better to define the entire text including the placeholder and fill it with data. You may have to adjust the formatting when converting the data. The Python string format methods can help you to do this.
How do Python string format methods work?
Python supports several string format methods, each with its own advantages and disadvantages. The number of methods has grown over time, which means that there are many similarities and some differences between the individual methods. We’ll present the four most popular approaches to Python string formatting:
- String formatting with the modulo operator
- String formatting with the str.format() method
- String formatting with string interpolation
- String formatting with template strings
Modulo operator
This oldest string formatting in Python uses the Python operator for percentage signs, which is used for the mathematical modulo operation. This approach is also known as modulo string formatting.
We’ll use a scheme with three variables in the following example. The actual names are not important and were chosen to make the code easier to follow.
Component | Designation |
---|---|
String template with placeholder(s) | Template |
Modulo operator | % |
Data to be inserted | Data |
Completed formatted string | message |
First, we must define the template as a string variable with the name template. We then define the placeholder’s position with the abbreviation %s in the template:
template = "The total price is %s"
PythonThen, we define the value to be inserted as a variable called data:
data = 69.51
PythonTo perform Python string formatting, we write the string template followed by the modulo operator and the data variable. We also need to assign the formatted string to the message variable:
message = template % data
PythonThe data can be a variable. But we can also use a literal, or an expression. The modulo operation can be placed on a single line. This is an example with a string literal instead of a placeholder:
message = "Here comes %s" % "Jack"
PythonIn the following example, we’ll calculate the sum of the individual prices and insert the total price into the message. The same scheme takes effect; showing that an expression may follow the modulo operator:
prices = (42, 69, 51)
message = "The total price is %s" % sum(prices)
PythonYou may be wondering why %s is used as the placeholder instead of just the percent sign. The “s” has a special meaning. It is converted into a string using the str() function when the data is inserted.
There are other string formatting shortcuts in Python, which focus on formatting numbers. To illustrate this, we’ll use pi in the following example. The number is placed into the placeholder %s. The output contains 15 decimal places:
from math import pi as PI
print("The value of Pi is approximately %s" % PI)
PythonIf we use the abbreviation %g as a placeholder instead, only five decimal places will be displayed:
print("The value of Pi is approximately %g" % PI)
PythonA number of other options are available, and we’ll describe these in the following section.
With Python’s modulo string formatting, you can also define and populate multiple placeholders. In this case, the modulo operator expects a tuple with as many values as there are placeholders. The placeholders are filled with the values:
person = ('John', 42)
message = "My friend %s is %s years old" % person
PythonThe Python string formatting with modulo operator is not very easy to read. It becomes simpler if we pass a dict with the data instead of a tuple. We must enter the dict entries’ names between the brackets after the placeholder’s percent sign. Doing this makes it easier to recognize values when reading the string template:
person = {'name': 'John', 'age': 42}
message = "My friend %(name)s is %(age)s years old" % person
Pythonstr.format() method
Python’s string format method is an improvement on the modulo formatting that was originally included in Python 3 and later carried over to Python 2.6. An object-oriented approach is used instead of a special operator. This makes str.format() consistent with the other string methods, such as Python’s str.split function.
The Python str.format() method’s basic scheme is similar to formatting with the modulo operator and can be found in a similar form in .NET and Rust. Curly braces are used as placeholders in the template string and the data is passed as arguments to the function call:
template = "Here comes {}"
data = 'Jack'
message = template.format(data)
PythonIf the template string contains multiple placeholders, a corresponding number of arguments must be passed:
message = "My friend {} is {} years old".format('John', 42)
PythonWe can choose to specify the position of the arguments to include them as indexes. This allows us to decouple the placeholders’ order from the arguments. Remember that indexes start at zero:
message = "My friend {1} is {0} years old".format(69, 'Jim')
PythonIf the individual values are in a data structure, we should use an asterisk operator before the argument to unpack the data structure. This works with lists and tuples:
person = ('John', 42)
message = "My friend {} is {} years old".format(*person)
PythonThe blank placeholders can get confusing with longer strings. It is better to use named arguments:
template = "My friend {name} is {age} years old"
message = template.format(name = 'Jack', age = 51)
PythonIf you have too many values, the argument list becomes long and confusing. It is better to combine the values in a dict and unpack the dict when calling the function. To unpack a dict, a double asterisk needs to be used:
person = {'name': 'Jim', 'age': 69}
# define string with placeholders
template = "My friend {name} is {age} years old"
# unpack dict in `format()` call
message = template.format(**person)
PythonThe wide range of formatting options is what makes formatting Python string with str.format() special. It extends the functionality of modulo formatting and is especially powerful for outputting text on the command line as well as when used for tabular data. Now, we’ll look at how to use the mechanism for formatting numbers.
Python ensures precise control when outputting numbers as text. For example, you can specify how the numbers’ signs should be displayed. The number of decimal places can also be specified for decimal numbers. This is useful when displaying positive or negative numbers in a table.
Let’s imagine we want to output temperature measurements:
samples = [('A0147D', 27.6), ('X1489M', -4.7), ('P9921U', -10.93)]
for sample in samples:
print('| {} |'.format(*sample))
PythonThe output looks chopped up because the numbers are different lengths:
| A0147D | 27.6 |
| X1489M | -4.7 |
| P9921U | -10.93 |
BashNow, we’ll customize the code and define a set of formatting options:
for sample in samples:
print('| {} | {: 6.2f} |'.format(*sample))
PythonThe output fits now:
| A0147D | 27.60 |
| X1489M | -4.70 |
| P9921U | -10.93 |
BashLet’s take a closer look at the formatting options of the second placeholder. These come after the colon and have four components:
- Space: Comes before positive numbers to compensate for the space taken by the minus sign for negative numbers
- Number before the dot: Total number of letters available for the number
- Number after the dot: Number of digits after the decimal point, fills with zeros if necessary
- Letter “f” before the closing curly bracket: Formats the number as a decimal number
There are a number of other formatting options that can be used to control the output of numbers and strings. They are not used very often, but if you need to use them, consult the official Python string formatting documentation.
String interpolation
String interpolation has been available as a formatting method since Python 3.6. The approach is also known as “f-strings” and it is the most convenient and performant for many scenarios. The name comes from the general syntax. F-strings start with the letter “f”, which is placed directly before the opening quotation marks.
Curly braces are used as placeholders. Unlike the methods presented so far, the placeholders contain an expression rather than an index or name. The placeholders are filled immediately when defining the f-string:
message = f "40 + 2 equals {40 + 2}"
PythonThe expression can also be a variable’s name:
name = 'Jack'
message = f "Here comes {name}"
PythonMultiple expressions can be included:
prices = (42, 69, 51)
currency = 'EUR'
message = f "The total price is {sum(prices)} {currency}"
PythonTemplate strings
Python also supports string formatting with template strings. These are generated from a separate class and provide protection against security vulnerabilities when formatting user strings.
The formatting options within template strings are limited. The placeholders may only contain variable names, which are used instead of expressions. Template strings are simple, and they are well suited to the internationalization of strings, which are available in multiple languages.
A dollar sign followed by the variable’s name is used as a placeholder. This makes template strings similar to a shell language’s syntax, such as Bash or Zsh. The value is inserted when the substitute() method is called:
# import `template` class from `string` module
from string import template
# instantiate template
template = Template("Hey there, I'm $name")
# perform substitution
message = template.substitute(name = 'Jack')
PythonAnother syntax for placeholder is the dollar sign followed by curly braces that contain the variable’s name. This allows substitutions to be made within a word:
template = Template("Let's buy $amount ${fruit}s")
message = template.substitute(amount = 3, fruit = 'orange')
PythonWhich Python string format method should I choose?
Below we have summarized the individual methods and included relevant details about each one:
Method | Scheme | Placeholder | Benefit |
---|---|---|---|
Modulo formatting | template % data | %, %(key) | Python version < 2.6 |
str.format() | template.format(data) | {}, {index}, {key} | Complex formatting |
f-string | f“{expression}“ | {expression} | Extensive strings |
Template string | template.substitute(data) | ${identifier} | If the strings come from the user side |