Python Forum
Strange syntax error with f-strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strange syntax error with f-strings
#1
Hello Python experts,

can someone please explain me why this code produces a syntax error:
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
}

print(f'Sarah\'s favorite language is {favorite_languages['sarah']}.')


On the other hand, the following code works fine:
print(f'Sarah\'s favorite language is {favorite_languages["sarah"]}.')


Another solution is:
print(f"Sarah\'s favorite language is {favorite_languages['sarah']}.")

Interesting, when using double quotes ["sarah"] code snippet works as intended.
I'm using Python 3.8.5.

I know that the problem is caused by "closing the string" but escape (\) doesn't solve it.
I don't like mixing " and ' when working with strings, but this time I need to make an exception or to use an auxiliary variable.
Reply
#2
In fact you found the solution yourself.
(Oct-16-2020, 08:55 AM)Askic Wrote: print(f'Sarah\'s favorite language is {favorite_languages['sarah']}.')

The reason: the quoted string ends with the first (un-escaped) quote. So this is the string Python sees:
'Sarah\'s favorite language is {favorite_languages['
And then Python would ask: what do you mean with the rest. Your solution is correct: you must use different quotes within the string.
metulburr likes this post
Reply
#3
Because in the one with the error, the f-string is enclosed in single quotes and you also enclose the key inside the string. As you have discovered you need to enclose the string or the key with double quotes.

so it parse it as f-string f'Sarah\'s favorite language is {favorite_languages[' then variable sarah and then string ']}.' And it's syntax error because there is no comma between them
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
in the first one, you have quotes within quotes, so use this format
print(f"Sarah's favorite language is {favorite_languages['sarah']}.")
Reply
#5
Thank you guys, I know what is the cause of the syntax error. I tried to use escape character '\' just like I would used in f'Sarah\'s... but it didn't work inside the bracket.

Since I prefer to use only single quote (') when working with strings, I solved the problem by using another auxiliary variable.
Reply
#6
You could always use the format method if you dont like mixes strings. I do on occasion when i have numerous variables and i want to see the layout in the string better when the variables within are cluttering it up.
print('Sarah\'s favorite language is {}.'.format(favorite_languages['sarah']))
ibreeden likes this post
Recommended Tutorials:
Reply
#7
Thank you metulburr!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,160 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 374 Jan-19-2024, 01:20 PM
Last Post: rob101
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Strange argument count error rowan_bradley 3 709 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Syntax error while executing the Python code in Linux DivAsh 8 1,545 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,297 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,243 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 882 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Help with Logical error processing List of strings dmc8300 3 1,081 Nov-27-2022, 04:10 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020