This lesson demonstrates the simple syntax of f-Strings. You’ll see that the syntax is similar to the one you used with str.format() but less verbose.
Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
The Simple Syntax of F-Strings
00:00 Now that you’ve looked at old-school string formatting, I’ll have you take a look at f-strings—the new, improved way to format strings. First I’ll show you simple syntax, then talk about arbitrary expressions, multiline f-strings, and then make speed comparisons.
00:15
f-strings are also known as formatted string literals. They have a very simple syntax. One big advantage is they are evaluated at runtime. And they are formatted using the __format__ protocol.
00:26
The basic format looks like an f at the beginning of your string, then inside curly braces, {}, you’ll have different expressions that will be evaluated at runtime.
00:36 Let me show you some examples. f-strings have a very simple syntax. If you put a couple of variables in, to build the string, inside of our curly braces, we’ll put our expressions—in this case, simply the names of these variables.
00:52
And, if you’d like, you can use capital F instead of a lowercase f at the beginning of your string
01:00 and it’ll work the same. Or it’ll look like—if there are even more—just the same.
01:14 Nice, clean, simple. Next, let me tell you a little more about these expressions.
Become a Member to join the conversation.
Raphael on March 13, 2019
Thank you for the lesson. I’m migrating our software from Python 2.x to Python 3.x. f-Strings are indeed nice, clean and simple as you said.
Regards, Raphael