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
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.
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).
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 .
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 ();
You could use an expression like this
str(myVar).lower() if type(myVar) is bool else myVar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With