How to nest if in print Python?

Question:

There is a boolean variable. I need to check its value and, depending on this, change the arguments passed to print

amusingsize = True
print ("Amusing size: %s") %(if(amusingsize) "GB" else "MB")

How can you do something like this? I don't want to fence in unnecessary if / else blocks.

Answer:

Here's another "hacker" option

print "Amusing size: %s" % ("MB", "GB")[amusingsize]
Scroll to Top