[Tutor] variable in format string throwing error
eryksun
eryksun at gmail.com
Tue May 14 00:17:43 CEST 2013
On Mon, May 13, 2013 at 5:51 PM, Jim Mooney <cybervigilante at gmail.com> 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
More information about the Tutor
mailing list