Lower case logical string

Can the str.format() method print boolean arguments without header lines?

I cannot use str(myVar).lower() as a format argument, because I want to keep case letters when myVar not logical.

Please do not submit solutions with conditional checks of variable values.

All that interests me is the ability to write the following:

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

so the output will be "Bla bla bla true" when myVar == True and "Bla bla bla false" when myVar == false

+5
source share
2 answers

You can use an expression like

 str(myVar).lower() if type(myVar) is bool else myVar 
+4
source

Try the lambda you can call:

 >>> f = lambda x: str(x).lower() if isinstance(x, bool) else x >>> 'foo {} {}'.format(f(True), f('HELLO')) 'foo true HELLO' 
+2
source

Source: https://habr.com/ru/post/1200577/


All Articles