Reputation: 678
I have a situation like this.
a={'x':'test'}
b="this is a %(a['x'])s
print b % {'test':'testing'}
the desired result is
>>this is a testing
but it throws an error "ValueError: incomplete format key"
Please suggest me a better way.Thanks in advance.
Upvotes: 3
Views: 370
Reputation: 12491
You can expand a dictionary to named parameters
a = {'test': 'testing'}
b = "this is a {test}".format(**a)
print b
output: this is a testing
The **
in front of the dictionary causes a parameter to be passed to the method which has the name that matches the key and the value matched the value in the dictionary.
Now looking at your code and what you are trying to achieve you are using two dictionaries. So you need to format your string twice.
a={'x':'test'}
b="this is a {{{x}}}".format(**a)
print b
print b.format(**{'test':'testing'})
output:
this is a {test}
this is a testing
The first format creates a new named place holder with the value of key x. The second format will populate the place holder.
Upvotes: 1
Reputation: 51
Why showing key error
In your code %(a['x']) will simply convert key argument to "a['x']"
This is equivalent to:
a={'x':'test'}
b="this is a %(a['x'])s
print b % {"a['x']":'testing'}
"this is a testing"
You can use format or % suggested by other answers
Upvotes: 1
Reputation: 4670
How about using format
:
>>> a = {'x': 'test'}
>>> print("this is a {0[x]}".format(a))
this is a test
How to use format
:
{field!convertflag:formatspec}
formatspec:
[[fill]align][sign][#][0][width][.precision][typecode]
I'll give you some example:
>>> import sys
>>> print("I'm using python {0.version}".format(sys))
I'm using python 2.7.9 (default, Mar 31 2015, 09:35:58)
[GCC 4.8.1]
>>> print("Hello, {name}".format(name="lord63"))
Hello, lord63
>>> print("{0}, {1}, {0}".format("one", "two"))
one, two, one
>>> print("The fifth: {0[4]}".format(range(1,6)))
5
>>> print("I'm {0[name]}".format(dict(name="lord63", age=20)))
I'm lord63
Upvotes: -1
Reputation: 14400
There's more problem with your code than you're telling. The string is missing the trailing "
, but also it's not quite clear what you're trying to do. What's the a
for?
What you need to do is to put the key in the parentheses, for example:
b="this is a %(test)s"
Or if you want to get the x
from a
and and then expand you'll need to do something like:
b="this is a %%(%(x)s)s" % a
This will expand the x
in a and result in the string in the former example. I used double percent to escape the percent sign.
Upvotes: 0
Reputation: 1779
You could do the exchange before inserting into string.
c = { 'test' : 'string' }
y = c[a['x']]
Then simply "mystring %s" % y
If you dont want to exchange the values before you could use
("{0[%s]}" % a['x']).format(c)
Upvotes: 1
Reputation: 6581
Python2
If you want to print this is a testing
, this is one way to do it:
a={'x':'test'}
b="this is a %sing" %(a['x'])
print b
Output:
this is a testing
Upvotes: -1
Reputation: 10223
Need to create one more dictionary.
>>> a={'x':'test'}
>>> b="this is a %s"
>>> c = {'test':'testing'}
>>> print b % c[a['x']]
this is a testing
Upvotes: 1