Learn about some of Python’s built-in methods that can be used on strings
Before PEP 498 was introduced, Python had primarily three ways of formatting strings i.e. the
str.format and the
string.Template. In 2015, Eric V. Smith proposed a new string formatting mechanism known as Literal String Interpolation, which provided a simpler and effective way for formatting strings. These strings are referred to as formatted string literal or f-strings as they are prefixed with
To begin, we’ll quickly look at the different string formatting styles in Python and how f-strings shines above the rest. Let’s say we have the names and ages of three people, and we want to print them in the format given below:
The manager is [NAME] and his age is [AGE].
Let’s now look at the comparison between different string formatting styles in Python. We’ll see the same output but utilizing different formatting.
1. Using % Operator
2. Using string. format
The use of
string.format did simplify the formatting to some extent.
3. Using f-strings
F-strings further simplify the formatting.
As seen above, the f-strings come in handy when formatting strings. No more figuring out the %s and the format keywords. They are straightforward, less ambiguous, and easy to understand. Since we now know about the usefulness of the f-strings, it’s time to look at some of their other capabilities too.
1. Formatting expressions with f-strings
F-strings allows you to put expressions between the brackets. The expression within the brackets is evaluated and displayed as a result.
Here the distance is calculated within the expression and this saves an additional step for the user.
2. Formatting datetime Objects
Well, the same results can also be obtained by merely using f-strings. This eliminates the need to use
3. Adding separators: commas, underscores, and spaces
F-strings can also come in handy in case you want to include separators in long numbers i.e separate numbers at thousands’ place. You can add a comma, an underscore, or simply separate the numbers by space.
4. Justifying and centering strings
By default, the strings are justified to the left. However, it is possible to justify them to the right by using
< characters followed by a number that denotes the padding.
Similarly, you can also center a string by using
^N after the string that has to be centered. Again N denotes the number of characters.
if-else Conditional in an F-String
f-strings are able to evaluate if-else conditions. You can specify the condition within the curly braces and it outputs the result. Here is an example to print the greater of the two given numbers.
We have seen that f-strings are pretty useful when it comes to formatting strings. We have also seen some of the ways in which they can be used. There are some other great uses of f-strings and in case you want to dig deeper, the official documentation is a great place to start. If this article has piqued your interest in the string datatype, here is another article that you might enjoy which is about the string methods in Python and their uses.