Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python SyntaxError: f-string: unmatched '['

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Pleas 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading