I had a similar issue. After removing parts of the code to narrow down the problem, I found the root cause.
In my case I was printing an f-string and inside the f-string I had a space in the name of the variable I was calling
var32 = x
print(f"This is a statement {var 23} "
Removing that space fixed the problem.