470,841 Members | 948 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,841 developers. It's quick & easy.

Null String Variable

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
Jul 19 '05 #1
4 16270
Rotary wrote:
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-

--
Klaus Alexander Seistrup
Magnetic Ink, Copenhagen, Denmark
http://magnetic-ink.dk/
Jul 19 '05 #2
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

Jul 19 '05 #3
Sean McIlroy wrote:
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
Jul 19 '05 #4
Sean McIlroy wrote:
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
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mark D'Souza via .NET 247 | last post: by
12 posts views Thread by Bernie V | last post: by
5 posts views Thread by Generic Usenet Account | last post: by
2 posts views Thread by bill | last post: by
reply views Thread by =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.