1. For this, I would like to ha...">

Python: "inline" block / condition to return char?

I would like to create a string that uses plural if count> 1.

For this, I would like to have an "inline" condition that returns an 's' to join with my noun.

print "The plural of plural is plural{0}. {1}".format( {'s' if count > 1}, "Isnt't it!?")

      

+2


a source to share


2 answers


You need to add the else part in 's' if count > 1

, otherwise it is not a valid expression
(because the value returned when count <= 1

not specified and Python cannot guess what it should do):



print "The plural of plural is plural{0}. {1}".format(
    's' if count > 1 else '', "Isnt't it!?")

      

+1


a source


print "The plural of plural is plural{0}. {1}".format('s' if count > 1 else '', "Isnt't it!?")

      



+1


a source







All Articles