Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)}"
like image 362
calypto Avatar asked Sep 11 '26 02:09

calypto


1 Answers

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

like image 179
user2357112 supports Monica Avatar answered Sep 12 '26 15:09

user2357112 supports Monica