Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python f-Strings: The Pesky Details

Give Feedback

You’ve learned a lot about f-strings and why they are great. In this lesson, you’ll see what details to keep in mind when working with the following inside f-strings:

  • Quotation marks
  • Dictionaries
  • Braces
  • Backslashes

00:00 For this next section, we’ll talk a little bit about some of the pesky details you need to keep in mind when working with f-strings. The first part will be about quotation marks and working with the different styles of them, then dictionaries.

00:12 Then we’ll talk about braces, backslashes, and lastly, inline comments. I’ll have you dive in. Okay, working with quotation marks. What forms of quotation marks will work? First off, you need to make sure that you’re not using the same type of quotation mark on the outside as you are using on the inside.

00:31 If you use double quotes (") on the outside of your expression and single quotes (') on the inside, that’ll work just fine. And the reverse, single quote with the double quote will work also—especially if you spell the name correctly.

00:49 But even if you don’t! And as you saw before, you can use triple quotes of either style.

01:01 If you find that you need to use the same type of quotation mark on the inside as the outside, then you need to escape it with the backslash (\).

01:11 For this one, we need to put the name in and the age.

01:19 So that would look something like The "comedian". So, some of these issues—as far as dealing with quotation marks—may cause some problems when working with dictionaries. So let’s build one.

01:40 If you start with the double quotes on the outside, when you address the dictionary and you use single quote on both the 'name' and 'age', that will evaluate correctly. But what happens if we don’t?

01:58 I’ll have you start your f-string in this example with a single quote.

02:08 And finally, end with a single quote out here. So, what happens? Well, the string ended right here. It ran up to this point and said invalid syntax because it closed the expression too early, and you end up with a SyntaxError.

02:27 So just make sure when using dictionaries to use the same form of quotation marks on the inside of your dictionary and make sure that you use a different on the outside. What about working with braces?

02:41 If you need braces to appear in your strings, you use double braces. Say you want the number 74 to appear with braces around it. By putting two braces in, it will print out the other pair.

02:55 What happens with triple? It still does only a single pair. However, if you are a big braces fan and you need double braces, if you go beyond three, it will do double.

03:10 As you saw earlier, you can use a backslash (\) to escape out certain types of characters. However, you can’t use a \ inside of the expression.

03:22 If you’re inside of the expression and you try to escape this quotation mark, it will get upset, as string expressions cannot include any backslashes. It’s very literal about it.

03:46 If you really needed to have quotation marks inside of here and you needed to escape out certain characters, you could create the expression in advance and pass it.

03:59 When it’s evaluating the expression, it can’t include the backslashes, but if they’re included in variables already, it will handle it without an issue. So, what about commenting your code?

04:10 If you were to put the pound sign (#) and start adding your comments inside of the expression, it will cause an error. You need to keep any comments you want to put outside of the expressions.

04:28 Great! Let me take you through a wrap-up.

Sciencificity on March 20, 2019

This also works here: f"{'Eric Idle}" :). Thanks for the lessons!

Levi on March 14, 2020

At around 1:50 when the dictionary was being used with the f string, it would be nice if a person could unpack the dictionary and use [‘name’] instead of having to reference the items as comedian[‘name’].

Become a Member to join the conversation.