Handle %-chars in invalid lines correctly

The use of string formatting for inserting the line number doesn't work well
if the line itself contains percents. This patch avoids the use of directly
formatting the text, and also cleans up some inconsistencies
(``at line "%s"`` vs `` at line %s``).