How do I escape slashes the string formatting operator? (or: why is it behaving this way?)

marek.rocki at wp.pl marek.rocki at wp.pl
Thu May 7 01:51:57 CEST 2009


Dustan napisał(a):
> Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> 'HELP!%(xyz)/' % {'xyz':' PLEASE! '}
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: unsupported format character '/' (0x2f) at index 11
> >>>
>
> It doesn't like the forward slash after the closed parentheses. I
> don't know what it is expecting, but I need that slash there
> (obviously this is a simplified example).
>
> All help appreciated.

Strign formatting docs (http://docs.python.org/library/
stdtypes.html#string-formatting) say that specifying a conversion type
is mandatory. So you actually should write something like:
'HELP!%(xyz)s/' % {'xyz':' PLEASE! '}



More information about the Python-list mailing list