Boolean to string with lowercase

tubafranz picture tubafranz · Aug 18, 2014 · Viewed 24.5k times · Source

Can the str.format() method print boolean arguments without capitalized strings?

I cannot use str(myVar).lower() as argument of format, because I want to preserve the case of the letters when myVar is not a boolean.

Please don't post solutions with conditional checks of the values of the variable.

All I am interested is in the possibility of writing the following:

"Bla bla bla {}".format(myVar)

so that the output becomes "Bla bla bla true" when myVar == True and "Bla bla bla false" when myVar == false

Answer

John La Rooy picture John La Rooy · Aug 18, 2014

You could use an expression like this

str(myVar).lower() if type(myVar) is bool else myVar