Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SyntaxError: f-string: unmatched '[' [duplicate]

Tags:

python

fastapi

from fastapi import FastAPI
from fastapi.params import Body

app = FastAPI()

@app.post("/createposts")
def create_posts(payload: dict = Body(...)):
    print(payload)
    return {"new_post" : f"title {payload["title"]} content: {payload["content"]}"}

I'm trying to create an API with Fastapi, but every time I run the code I get this error related to the return statement: SyntaxError: f-string: unmatched '['

Thank you!

like image 854
Bennet Weber Avatar asked Aug 28 '26 12:08

Bennet Weber


1 Answers

Please change

return {"new_post" : f"title {payload["title"]} content: {payload["content"]}"}

to

return {"new_post" : f"title {payload['title']} content: {payload['content']}"}

You can't have " quotes inside f"..."

The error says that after the first [ the string stops and breaks.

like image 98
EvgenyKolyakov Avatar answered Aug 31 '26 04:08

EvgenyKolyakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!