Search code examples
pythonpython-3.xerror-handlingf-string

How do I handle errors in an f-string?


I am attempting to handle errors within an f-string, but my attempts don't seem to work. Is there a way to accomplish this?

string = f"For example I thought this syntax would work but it doesn't {try: 5/0 except Exception: str(infinity)}"

Solution

  • You can't. Move the exception handling out of the f-string.