Null String Variable

R

rotary

I have a variable that store a string (i.e. a variable name msg).I want to
say something like that: if msg is empty ...then do something.So how can i
figure that msg is empty string (no character, msg = '').Maybe a silly
question but i have no programming experiense.Thanks, Leonidas
 
K

Klaus Alexander Seistrup

Rotary said:
I want to say something like that: if msg is empty ...then do
something. So how can i figure that msg is empty string (no
character, msg = '').

#v+

if not msg:
print 'msg is empty'

#v-
 
S

Sean McIlroy

well, somebody's already pointed out that bool(msg)==False iff msg==''.
i'm curious to know what's wrong with simply writing

if msg=='':
## do_something
 
P

Peter Hansen

Sean said:
well, somebody's already pointed out that bool(msg)==False iff msg==''.

I may not have received the message yet, but if "iff" still means "if
and only if", then that statement is wrong. There are, of course, about
a dozen things for which bool(msg) is False when msg != '', not
including the possibility of objects that define their bool state
however they want.
i'm curious to know what's wrong with simply writing

if msg=='':
## do_something

I'm not sure anything is wrong with it in this case, though the idiom in
Python is generally to do just "if not msg" unless you're concerned
about the possibility that msg is not actually a string.

-Peter
 
S

Steve Holden

Sean said:
well, somebody's already pointed out that bool(msg)==False iff msg==''.
i'm curious to know what's wrong with simply writing

if msg=='':
## do_something
You would write this if there was a possibility that msg could be
something other than a string, whose value might also be interpreted as
False by the "if" (e.g. the None object, or an empty list) even though
your application wanted to handle it differently from an empty string.

regards
Steve
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top