Python’s F-Strings Are A Lot More Useful Than You Might Have Thought
Some cool things most people do not realize f-strings can do in Python

Introduction
We are all likely familiar with the string; a core data-type found in most programming languages since essentially the beginning of computing history. Strings are great, as they are able to represent a series of characters in either ASCII or Unicode that are very tangible to humans. As humans, we might not speak the language of zeros and ones that make up a unicode character, but a string is entirely understandable.
If the programming language in which you program is at least somewhat high-level in nature, then you are probably familiar with the concept of string interpolation, as well. String interpolation is the ability for a programming language to read and modify strings just by defining them. In Python, string interpolation like this is done through a feature called the F string. F strings allow you to do a lot with strings, and some people do not even realize the full capabilities of F-strings in Python. Luckily, there are random bloggers on the internet who can take a deep dive into F-strings for your brain, and that is what I am going to do today. If you would like to interact with the code that is going to follow, the notebook I used for this little project is also available on Github here:
What is an F string?
Before we go over a synopsis of what we can do with F strings, it is probably important that we briefly touch on what an F string actually is. In Python, normal strings are not interpolated. This certainly could help the language with some latency, given that it is interpreted and such. I think that is also kind of a good thing, as there is no reason for implicit interpolation, and it could potentially cause more problems than it solves. In Julia, for example, strings are always interpolated, so there is a chance that you could be trying to make a string that says
x = 5
mystr = "and I said for $x dollars in my wallet, I will buy one icecream"And accidentally end up with $5 when you want x dollars. Not sure how often all of that is going to coincide, but it is certainly something that could happen. Anyway, F strings in Python are denoted by simply providing an F before your string, like so:
f""The most obvious usage of F strings is to interpolate simple values inside of throws, output, what have you. This is done using the {} syntax and just providing that values name. The value will also be implicitly casted if it is possible, of course.
x = 5
f"One icecream is worth {x} dollars"This produces a string that actually looks more like this:
'One icecream is worth 5 dollars' More on F strings
We have established that F strings can be used to interpolate Python code, but what all is this capable of? One notable thing that we can do with F strings is also provide a “ this = that” kind of interpolation, which might be useful for debugging and error handling. We do this by just adding an equal sign:
f"If you are wondering what x is, {x=}"'If you are wondering what x is, x=5'Another thing we can take advantage of is something called conversions. Conversions allow us to apply functions to the interpolated values before they are pretended, examples of these include
- !r — Shows the string delimiter, calls the repr() method.
- !a — Shows the Ascii for the characters.
- !s — Converts the value to a string.
Each of these can be valuable, and they work by just calling !r at the end, often !s is not going to be needed as usually we print data-types, which as I touched on are converted implicitly, not typical structures, but in some cases you might need to provide this. Here is an example of usage with the repr version:
food = "cabbage"
food2brand = "Mcdonalds"
food2 = "French fries"f"I like eating {food} and {food2brand} {food2!r}""I like eating cabbage and Mcdonalds 'French fries'"We can also change the format of our interpolated value using object formatting. Every object in Python can determine a way that their format is represented in strings. In order to denote this is what we want to do, we use a colon followed by the format we would like to add, for example here we do this for date-time.
import datetime
date = datetime.datetime.utcnow()
f"The date is {time:%m-%Y %d}"'The date is 02-2022 15'This calls the __format__ method, which is provided to at least most classes.
Conclusion
So there are some of the great things that you can do with F strings. Generally, most people consider that F strings are quite limited in capability, but they really are capable of a lot of different things. Strings are a very important concept in Python, and while Python might not be the best language for meta-programming and parsing strings as code, there are certainly use cases in that region of Python as well. Thank you very much for reading, and happy interpolating!






