Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python exception message formatting [closed]

Question: Why don't I get an exception message when formatting the message with %s but I do with format?

Fails:

>>> Exception('foo %s', 'bar').message
''

Works:

>>> Exception('foo {}'.format('bar')).message
'foo bar'

Any explanation why it fails on %s?

like image 212
ezdazuzena Avatar asked Jul 29 '26 22:07

ezdazuzena


1 Answers

Your syntax for the %-substitution in Exception is incorrect. You need to use % to specify the replacement string:

>>> Exception('foo %s' % 'bar').message
'foo bar'
like image 188
Dartmouth Avatar answered Jul 31 '26 19:07

Dartmouth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!