This works:
print "Hello World%s" %"!"
But this doesn't
print "Hello%20World%s" %"!"
the error is ValueError: unsupported format character 'W' (0x57) at index 8
I am using Python 2.7.
Why would I do this? Well %20
is used in place of spaces in urls, and if use it, I can't form strings with the printf formats. But why does Python do this?
You could escape the % in %20 like so:
print "Hello%%20World%s" %"!"
or you could try using the string formatting routines instead, like:
print "Hello%20World{0}".format("!")
http://docs.python.org/library/string.html#formatstrings
You could escape the % with another % so %%20
This is a similar relevant question Python string formatting when string contains "%s" without escaping
I was using python interpolation and forgot the ending s
character:
a = dict(foo='bar')
print("What comes after foo? %(foo)" % a) # Should be %(foo)s
Watch those typos.
You might have a typo.. In my case I was saying %w where I meant to say %s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With