Printing out a fixed string like “Hello World” is straightforward, but usually not very valuable. Useful Python programs make use of string interpolation — that is, they include values of one or more variables in their output.
String formatting is the standard approach to safely and conveniently printing out strings that contain values from a program. In this article, we explore Python string formatting and show examples of how it’s done in Python programs.
What Is String Formatting?
String formatting involves taking a set of variables and, using an expression, formatting them into a provided string that contains placeholders for those variables. Python uses two different styles of string formatting: the older Python 2 style that’s based on the modulo operator (%), and the newer Python 3 style that uses curly braces and colons.
Python’s standard string formatting uses the modulo operator (the percent sign) as a special symbol to indicate different types of formats. There’s a high degree of similarity between the % operator and the printf format string functions in the C programming language.
Here’s an example of Python string formatting:
The %d and %s values here work as keywords to indicate where the variables after the % operators need to be placed.
We’ll now cover how to use argument specifiers, such as %s and %d from the example above. While we’ll specifically focus on converting strings and numbers, you’ll need to read the official Python guidelines on formatting to get more in-depth information on string modulo operators and conversion types.
Best Practices for Using %s and %d in Python
Conversion specifiers such as %s and %d appear in the format string as “placeholders” along with normal text. These specifiers dictate how the operation will format the inserted values.
Let’s now revisit the first example we looked at, this time focusing on conversion specifiers in the format string — in this case, %d and %s.
Upon inspecting the first line, you’ll notice that %d in the format string corresponds to a number in the tuple, while the following %s specifiers correspond to string values in the tuple. Statements like %s and %d are known as conversion types, or signifers that tell the operator what kind of value to expect in the tuple.
While a conversion specifier’s basic structure looks like %<type>, it can also contain any of the components referred to as flags, width and precision:
These components allow for finer control over the display of certain conversion types, but remain optional. Only the modulo symbol and the conversion type are required. While elaborations on many different conversion types may be found in this Real Python tutorial, we’ll focus exclusively on two popular conversion types: %s and %d.
How to Use %s
There are three types of formatting in Python that pertain to strings. While s, r and a produce string output using the built-in functions str(), repr() and ascii(), respectively, the s is by far the most called upon. In fact, you’ll almost always use the python s when inserting a string:
Under the hood, %s tells the formatter to call the str() function on the argument to essentially perform str(arg).
When using multiple substitutions — strings or otherwise — in a tuple, it’s important to remember that the strings’ position in the tuple will influence where they’ll be inserted. For example, if we switch around the strings in our example tuple, we’ll get output that doesn’t make sense:
You could also save a string to a variable and use that variable name in the tuple:
Note how there’s no tuple in the above example; that is, the variables are not passed as (state). Recall that the modulo operator only takes one argument. It’s usually in the form of a tuple, but if you have only one substitution, the single variable alone can act as the argument.
How to Use %d
When you need to convert a number, chances are you’ll be using %d. The stands for decimal. As with strings, there are several types that indicate numbers: d, i and u are functionally equivalent. They all convert the specified argument to a string representation of a numeric or decimal value, saving you from having to cast str(int(arg)):
Of the three types, you’ll almost always use %d for casting numbers as strings in Python string formatting. The technical difference between %s and %d is simply that %d calls int() on the argument before calling str().
Of course, you can’t call int() on a string, or on anything else but a number. So if you do use multiple types in your operation but mix up the ordering of your tuple, you’ll generate an exception:
This will not print ‘Hawaii went to 7 goats’, but will instead result in an error. This is because Python recognizes that ‘Hawaii’ is not a number.
More Usage Scenarios for %s and %d
Let’s now look at more complex conversions for both %s and %d using dictionaries.
Remember how the ordering in the tuple matters when passing in several values to your format string? If you do opt for multiple substitution, the <values> you insert into the format string need not be limited to a tuple. If you’d rather use a dictionary to track variables, you can pass a mapping to the operator as its argument and use variable substitutions by name in your format string:
Note how the modulo symbol precedes a conversion specifier that’s one of the dictionary keys in parentheses. This key name is in turn followed by the conversion type. Using this technique, you can specify the inserted values in any order:
Now that we have a good understanding of Python string formats using the modulo operator, let’s look at a few common mistakes and ways we can avoid them.
Common Errors Using %s and %d
A series of common errors may occur when using %s. We already went over how messing up the order of values in tuples can cause exceptions due to type incompatibilities, but there are other tuple- and dictionary-related mixups that can generate error. These are usually related to length issues in tuples and misnamed variables in dictionaries.
These common mistakes, coupled with newer string formatting alternatives, have led Python to deemphasize the “old style” of modulo formatting, though it’s still supported by Python’s latest versions and that’ll be the case for the foreseeable future.
Even if you do prefer the modulo operator, it’s a good idea to be aware of the other options at your disposal, so as to avoid errors.
Python 3.6 introduced a new Python string format called the formatted string literal, or f-strings. An f-string resembles a regular string save for two features: it’s prepended by the character f, and uses enclosed expressions — that is, variables in curly braces that will be replaced in the output:
At runtime, the expression is converted to string representation and inserted into the original string in that location.
This function represents the “new style” of string formatting. Using this method, calling .format() on a string object produces the desired format:
Using the format function gets complex; you’re able to use index positions, type specifiers and keyword arguments:
There are some discrepancies between some of the Python .format() presentation types and string modulo operator conversion types, but are too minor for the scope of this article.
In this article, we went over the importance of string formatting, as well as how to use the modulo operator and the best practices for using the %s and %d conversion types. We also touched on newer string formatting methods that Python developers have at their disposal.
String formatting is just one complex, yet rewarding skill you’ll need to master as a Python programmer. With so much valuable information and world-class instruction available online, there’s no reason for you to fall behind. In our course on Introduction to Programming, we cover string formatting and various other tools you’ll need to thrive in the world of Python.