On Mon, May 13, 2013 at 5:51 PM, Jim Mooney <[email protected]> wrote:
> I'm trying variable substitution in a format string that looks like one that
> works, but I get an error. What am I doing wrong? tks
>
> x = 40
> s = 'John flew to the {0:-^{x}} yesterday'
> print(s.format('moon', x))
>
> Error is builtins.KeyError: 'x'

You need to use a keyword argument, x=x:

    >>> print(s.format('moon', x=x))
    John flew to the ------------------moon------------------ yesterday

Or change it to use a positional argument:

    >>> s = 'John flew to the {0:-^{1}} yesterday'
    >>> print(s.format('moon', x))
    John flew to the ------------------moon------------------ yesterday
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to