hobbes3 hobbes3 - 3 months ago 5
Python Question

Is there a way to add a conditional string in Python's advance string formatting "foo {}".format(bar)?

For example I have a line of code like this

if checked:
checked_string = "check"
else:
checked_string = "uncheck"

print "You can {} that step!".format(checked_string)


Is there a shortcut to this? I was just curious.

Answer
print "You can {} that step!".format('check' if checked else 'uncheck')