Returning a string from a boolean

D

Dan Rawson

I need to return "True" or "False" strings for a boolean value (mostly for display purposes).

It's obviously trivial to write the function:

def bStr (bVar):
if bVar:
return 'True'
else:
return 'False'

In Perl I can do this with the ternary 'if'

(bVar) ? 'True' : 'False'


Is there a simpler way in Python??

If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance of going to 2.3 in the near future <g>; I know
that some of this has changed in 2.3.

TIA . . .

Dan
 
D

Duncan Booth

In Perl I can do this with the ternary 'if'

(bVar) ? 'True' : 'False'


Is there a simpler way in Python??

If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
of going to 2.3 in the near future <g>; I know that some of this has
changed in 2.3.
Python 2.2 and earlier, the shortest way is:

return bVar and 'True' or 'False'
or
return ('True','False')[not bVar]

Both of the above will test the truth value of bVar, so for example an
empty string or empty list will return False. Personally, I would go for
your original function as combining clarity with reasonable but not
excessive brevity.

In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
but only if bVar is a bool.
 
D

Dan Rawson

Duncan said:
In Perl I can do this with the ternary 'if'

(bVar) ? 'True' : 'False'


Is there a simpler way in Python??

If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
of going to 2.3 in the near future <g>; I know that some of this has
changed in 2.3.

Python 2.2 and earlier, the shortest way is:

return bVar and 'True' or 'False'
or
return ('True','False')[not bVar]

Both of the above will test the truth value of bVar, so for example an
empty string or empty list will return False. Personally, I would go for
your original function as combining clarity with reasonable but not
excessive brevity.

In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
but only if bVar is a bool.

OK, thanks. I tend to use the one-line version because I can stick it inside (for example) a 'print' statement.
 
B

Bengt Richter

In Perl I can do this with the ternary 'if'

(bVar) ? 'True' : 'False'


Is there a simpler way in Python??

If it makes a difference, I'm using 2.2.2 (on Solaris) with no chance
of going to 2.3 in the near future <g>; I know that some of this has
changed in 2.3.
Python 2.2 and earlier, the shortest way is:

return bVar and 'True' or 'False'
or
return ('True','False')[not bVar]

Both of the above will test the truth value of bVar, so for example an
empty string or empty list will return False. Personally, I would go for
your original function as combining clarity with reasonable but not
excessive brevity.

In Python 2.3, str(bVar) will give you 'True' or 'False' as appropriate,
but only if bVar is a bool.

which you can ensure by str(bool(anyvar)) ;-)

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top