☞ Indexing option:
When accessing arguments’ items in format string, you should use index to call the value:
yes = True
print 'no [{0[0]}] yes [{0[1]}]'.format((" ", "x") if yes else ("x", " "))
{0[0]}
in format string equals (" ", "x")[0]
in calling index of tulple
{0[1]}
in format string equals (" ", "x")[1]
in calling index of tulple
☞ *
operator option:
or you can use *
operator to unpacking argument tuple.
yes = True
print 'no [{0}] yes [{1}]'.format(*(" ", "x") if yes else ("x", " "))
When invoking *
operator, 'no [{0}] yes [{1}]'.format(*(" ", "x") if yes else ("x", " "))
equals 'no [{0}] yes [{1}]'.format(" ", "x")
if if statement is True
☞ **
operator option (It's extra method when your var is dict):
yes = True
print 'no [{no}] yes [{yes}]'.format(**{"no":" ", "yes":"x"} if yes else {"no":"x", "yes":" "})