If you’re writing modern Python code with Python 3, you’ll probably want to format your strings with Python f-strings. However, if you’re working with older Python codebases, you’re likely to encounter the string modulo operator for string formatting.
If you’re reading or writing Python 2 code, it’ll help if you’re familiar with this technique. Because the syntax still works in Python 3, you might even see developers use it in modern Python codebases.
In this tutorial, you’ll learn how to:
Use the modulo operator (%) for string formatting
Convert values into specific types before inserting them into your string
Specify the horizontal space a formatted value occupies
Fine-tune the display using conversion flags
Specify values using dictionary mapping instead of tuples
If you’re acquainted with the printf() family of functions of C, Perl, or Java, then you’ll see that these don’t exist in Python. However, there’s quite a bit of similarity between printf() and the string modulo operator, so if you’re familiar with printf(), then a lot of the following will feel familiar.
On the other hand, if you aren’t familiar with printf(), don’t worry! You don’t need any prior knowledge of printf() to master modulo string formatting in Python.
Free Bonus: Click here to get our free Python Cheat Sheet that shows you the basics of Python 3, like working with data types, dictionaries, lists, and Python functions.
Use the Modulo Operator for String Formatting in Python
You’ve probably used the modulo operator (%) before with numbers, in which case it computes the remainder from a division:
2
With string operands, the modulo operator has an entirely different function: string formatting.
Note: These two operations aren’t very much alike. They only share the same name because they are represented by the same symbol (%).
Here’s what the syntax of the string modulo operator looks like:
On the left side of the % operator, <format_string> is a string containing one or more conversion specifiers. The <values> on the right side get inserted into <format_string> in place of the conversion specifiers. The resulting formatted string is the value of the expression.
Get started with an example where you call print() to display a formatted string using the string modulo operator:
6 bananas cost $1.74
In addition to representing the string modulo operation itself, the % character also denotes the beginning of a conversion specifier in the format string—in this case, there are three: %d, %s, and %.2f.
In the output, Python converted each item from the tuple of values to a string value and inserted it into the format string in place of the corresponding conversion specifier:
The first item in the tuple is 6, a numeric value that replaces %d in the format string.
The next item is the string value “bananas”, which replaces %s.
The last item is the float value 1.74, which replaces %.2f.
The resulting string is 6 bananas cost $1.74, as demonstrated in the following diagram:
If there are multiple values to insert, then they must be enclosed in a tuple, as illustrated above. If there’s only one value, then you can write it by itself without the surrounding parentheses:
Hello, my name is Graham.
Notice also that string modulo operation isn’t only for printing. You can also format values and assign them to another string variable:
Read the full article at https://realpython.com/python-modulo-string-formatting/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]