hobbes3 hobbes3 - 1 year ago 65
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"
checked_string = "uncheck"

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

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

Answer Source
print "You can {} that step!".format('check' if checked else 'uncheck')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download