Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean to string with lowercase

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

like image 924
tubafranz Avatar asked Aug 18 '14 10:08

tubafranz


People also ask

Can you convert a Boolean to string?

We can convert boolean to String in java using String. valueOf(boolean) method. Alternatively, we can use Boolean. toString(boolean) method which also converts boolean into String.

Can we convert boolean to string in Java?

To convert Boolean to String in Java, use the toString() method. For this, firstly, we have declared two booleans. String str1 = new Boolean(bool1). toString(); String str2 = new Boolean(bool2).

How do you convert a Boolean to a string in Python?

Convert bool to string: str() You can convert True and False to strings 'True' and 'False' with str() . Non-empty strings are considered True , so if you convert False to strings with str() and then back to bool type with bool() , it will be True .

How do I cast a boolean to a string in C#?

ToString() Method. This method is used to convert the value of this instance to its equivalent string representation i.e. either “True” or “False”. Syntax: public override string ToString ();


1 Answers

You could use an expression like this

str(myVar).lower() if type(myVar) is bool else myVar
like image 69
John La Rooy Avatar answered Sep 20 '22 22:09

John La Rooy