On 4/16/2012 17:14, Jon Clements wrote:
On Monday, 16 April 2012 11:03:31 UTC+1, Kiuhnm  wrote:
On 4/16/2012 4:42, Steven D'Aprano wrote:
On Sun, 15 Apr 2012 23:07:36 +0200, Kiuhnm wrote:

This is the behavior I need:
       path = path.replace('\\', '\\\\')
       msg = ". {} .. '{}' .. {} .".format(a, path, b)
Is there a better way?


This works for me:

a = "spam"
b = "ham"
path = r"C:\a\b\c\d\e.txt"
msg = ". %s .. %r .. %s ." % (a, path, b)
print msg
. spam .. 'C:\\a\\b\\c\\d\\e.txt' .. ham .

I like this one. Since I read somewhere that 'format' is preferred over
'%', I was focusing on 'format' and I didn't think of '%'.
Anyway, it's odd that 'format' doesn't offer something similar.

Kiuhnm

If you look at http://docs.python.org/library/string.html#format-string-syntax

you'll notice the equiv. of %r is {!r}

I thought that '%r' stood for 'raw' and not 'repr'. I didn't even notice that Steven removed the single-quotes from msg. I don't even need to escape single-quotes then: repr() does exactly what I need.

Kiuhnm
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to