Question
Does anybody have a beginner-friendly explanation as to why I get a SyntaxError when I call .get()
from within an f-string in Python >3.8?
Problem
When I call dict.get()
directly within the f-string I get a SyntaxError.
If I store the dict.get()
call in a variable and reference the variable in the f-string it works without error.
This works
def new_item():
desc = request.form.get('description', 'No description found')
return f'Your new item to insert\nDescription:{desc}'
http://127.0.0.1:5000/new_item displays:
This doesn't work
def new_item():
return f'Your new item to insert\nDescription:{request.form.get('description', 'No description found')}'
SyntaxError: invalid syntax
File ".\server.py", line 39
return f'Your new item to insert\nDescription:{request.form.get('description', 'No description found')}'
^
My Research
StackOverflow is littered with questions (here, here, here, here) and/or problems that were solved simply by calling an adequate Python version. I do have such a Python version installed and am calling also the appropriate command (in fact, the f-string works in an example above). Anyways, here's the powershell:
> python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32