A Guide to Everything String Formatting in Python | by Sara A. Metwalli | Towards Data Science

Strings are one of the most essential and used datatypes in programming. It allows the computer to interact and communicate with the world, such as printing instructions or reading input from the user. The ability to manipulate and formate strings give the programmer the power to process and handle text, for example, extract user input from websites forms, data from processed text, and use this data to perform activities such as sentimental analysis. Sentimental analysis is a subfield of natural language processing that allows the computer to classify emotions — negative, positive, or neutral — based on a piece of text.
“All sentiment is right; because sentiment has a reference to nothing beyond itself, and is always real, wherever a man is conscious of it.” — David Hume
Python is one of the main programming languages used to analyze and manipulate data next to R. Python is used in data analytics to help access databases and communicate with them. It is also useful in importing and exporting data using different web scraping techniques. Once this data is obtained, Python is used to clean and prepare data sets before they are fed to a machine-learning algorithm.
There are five ways you can use Python to format strings: formatting strings using the modulo operator, formatting using Python’s built-in format function, formatting using Python 3.7 f-strings, formatting manually, and finally using template strings. We will cover each one of these methods as well as when to use each of them.
Let’s get right to it...
If you wrote code using C or C++ before, this would look very familiar to you. In Python, we can use the modulo operator (%) to perform simple positional formatting. What I call placeHolder here, represents the control specs for formatting and converting the string. The % tells the interpreter that the following characters are the rules for converting the string. The conversion rules include information about the desired width of the result formatted string, the type of the result, and the precision of it — in case of floats.
This type of string formatting is sometimes referred to as the “old style” formatting. The placeHolder can refer to values directly or can be specified using keywords arguments.
formatted_str = 'Yesterday I bought %d %s for only %.2f$!' % (10, 2'bananas',2.6743)
If we try to print this string, we will get Yesterday I bought 10 bananas for only 2.67$! .
As you can see here, I had three placeHolders for three values, the first integer, the string — bananas — and the price of the bananas, which is a float. When referring to the float, I decided that a precision of two is all I needed; that’s why only 67 is displayed after the decimal point.
In the previous example, we replaced the placeHolders with the values directly. There is another way we can achieve this mapping, which is through using keyword arguments or by passing a dictionary.
name = 'John'balance = 235765formatted_str = 'Hello %(name)s, your current balance is %(balance)6.0f dollars.' % {"name":name, "balance":balance}
Using this method, we need to use the {} instead of the (). Because of that, we can also use a dictionary directly to fill in the placeHolders.
dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = 'I got %(quantity)d %(name)s for $%(price).2f$' % dictionary
The complete code for this section:
In Python 3, a new method to regulate the % operator syntax was introduced to the language. This is done using the built-in format method, also known as the “new style” formatting. Using the format method has many commonalities with using the % operator. Let’s consider rewriting some of the examples from before:
name = 'John'balance = 235765formatted_str = 'Hello %(name)s, your current balance is %(balance)6.0f dollars.' % {"name":name, "balance":balance}
Will become:
name = 'John'balance = 235765formatted_str = 'Hello {name:s}, your current balance is {balance:6.0f} dollars.'.format("name":name, "balance":balance)
We can summarize the differences between the % operator syntax and the format method as:
Using the % operator, we use the () inside the string, while with format method we use {} .To define the type of the conversion in the % operator, we set it right after the %, while in the format function we set the conversion after : inside the {}.
Using the % operator, we use the () inside the string, while with format method we use {} .
To define the type of the conversion in the % operator, we set it right after the %, while in the format function we set the conversion after : inside the {}.
However, if we want to pass a dictionary to the format string, we will need to modify the reforestation slightly. Instead of passing the name of the dictionary, we need to pass a pointer to the dictionary, for example:
dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = 'I got {quantity:d} {name:s} for {price:.2f}$'.format(**dictionary)
Moreover, we can use the same conversion rules of the % operator that can be used in the format method as well. In Python 3 and up to 3.6, the format method is preferred to the % operator approach. The format method presented a powerful approach to format strings; it didn’t complicate the simple case — such as the examples above — but, at the same time providing new levels to how we format strings.
The complete code for this section:
This, in my opinion, the fanciest and most elegant method to format strings. The f-string was presented in Python 3.8 as a more straightforward approach to string formating. Before we get into f-strings, let’s quickly cover the three types of strings in Python 3.8 and up.
Normal/ standard strings: contained between “” or ‘’, example “Hello, world!”. These are the most commonly used string type.Raw strings: lead by r”, for example r’hello\nworld’. In this type of strings, no processing is done on the string. Which means, escape characters don't work in these strings. Instead, they will be printed without any processing. This type of strings is mostly used in regular expressions.f-strings: lead by f”, for example, f’hello John!’. F-strings as used as an alternative way to format strings.
Normal/ standard strings: contained between “” or ‘’, example “Hello, world!”. These are the most commonly used string type.
Raw strings: lead by r”, for example r’hello\nworld’. In this type of strings, no processing is done on the string. Which means, escape characters don't work in these strings. Instead, they will be printed without any processing. This type of strings is mostly used in regular expressions.
f-strings: lead by f”, for example, f’hello John!’. F-strings as used as an alternative way to format strings.
towardsdatascience.com
f-strings present a simple way to format strings by directly embedding expression into the string. Let’s revisit the examples from before, one more time.
name = 'John'balance = 235765formatted_str = 'Hello {name:s}, your current balance is {balance:6.0f} dollars.'.format("name":name, "balance":balance)
If we want to rewrite this formatting using f-strings, it will look like:
name = 'John'balance = 235765formatted_str = f'Hello {name}, your current balance is {balance} dollars.'
One way f-string simplifies string formatting is, we can directly pass iterables — dictionaries, lists, etc. — to the string, as follows:
dictionary = {'quantity': 10, 'name': 'bananas', 'price': 2.6743}formatted_str = f'I got dictionary[quantity] dictionary[name] for dictionary[price]$'#f-string with listsmyList = [1,2,3,4]formatted_str = f'The last item of the list is myList[-1]'
You can also perform simple arithmetic or logic operations within an f-string directly.
#ArithmeticmyList = [1,2,3,4]formatted_str = f'The sum of the last and first items of the list is myList[0] + myList[-1]'#Logicformatted_str = f'myList[0] == myList[-1]'
f-strings provide much more freedom to the options we can use inside the string we want to format, such as list indexing, calling list or dictionary methods, or general iterables functions — e.g., len, sum, min, and max. We can also use conditional statements in an f-string:
age = 21formatted_str = f'You {"can't" if age < 21 else "can"} drink!'
The complete code for this section:
Manual formatting refers to using standard string methods to format strings. Mostly, we use the rjust, ljust, and center to justify the strings. The rjust method right-justifies a string given a specific width by padding it with spaces on the left. The ljust adds padding to the right and the center method adds padding on both sides of the string. These methods do not modify the original string; they instead return a new modified string. There is another string method that can be used to format strings, which is the zfill method. This method pads a string with a numeric value with zeros on the left.
Template strings are an alternative to the % operator, but instead of the %, template strings use the $ sign. Both methods provide what we refer to as substitution formatting. Template strings are not built-in in Python, hence to use them, we need to first import the string module. Template strings have three rules:
$$ is an escape character.Whatever follows the $ sign directly is the placeHolder for the template. By default, these placeHolders are restricted to any case-insensitive alphanumeric string. The first non-placeHolder character after the $ sign terminates this placeHolder specs, for example, a whitespace character.
$$ is an escape character.
Whatever follows the $ sign directly is the placeHolder for the template. By default, these placeHolders are restricted to any case-insensitive alphanumeric string. The first non-placeHolder character after the $ sign terminates this placeHolder specs, for example, a whitespace character.
These rules ad more are already defined in the string module, so once imported, you can go ahead and use them without the need to define them.
The template string has two main methods: the substitute method and the safe_substitute method. Both methods take the same argument, which is the substitute mapping. Moreover, both methods return a new formatted string, you can think of the template object as the blueprint for how future strings will be formatted. For example:
from string import Templateformatted_str = Template('Hello $who, your current balance is $how dollars.')formatted_str.substitute(who='John', how=235765)
To see the difference between the substitute and the safe_substitute methods, let’s try using a dictionary as our substitution mapping.
from string import Templatemapping = dict(who='John', how=235765)formatted_str = Template('Hello $who, your current balance is $how dollars.')Returned_str = formatted_str.substitute(mapping)
Assume our dictionary doesn’t have all the keys we need for the mapping, using the substitute method will give a keyError. Here's where safe_substitute comes to the help, if this method didn't find the correct key, it will return a string with the placeHolder for the wrong key. For example, this code:
from string import Templatemapping = dict(who='John')formatted_str = Template('Hello $who, your current balance is $how dollars.')Returned_str = formatted_str.safe_substitute(mapping)
Will return Hello John, your current balance is $how dollars. where if I had used the substitute method, I would've gotten an error.
The complete code for this section:
In this article, we covered five different ways you can use to format a string in Python:
The modulo operator.The format function.f-strings.Manual formatting.Template strings.
The modulo operator.
The format function.
f-strings.
Manual formatting.
Template strings.
Each method has its usage advantages and disadvantages. A general rule of thumb, as put by Dan Badar, if the string you’re formatting is user-obtained, template strings are the way to go. If not, use f-strings if you’re on Python 3.7 or the format function elsewhere.