Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 02:20 AM
rotary
Guest
 
Posts: n/a
Default 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


  #2  
Old July 19th, 2005, 02:20 AM
Klaus Alexander Seistrup
Guest
 
Posts: n/a
Default Re: Null String Variable

Rotary wrote:
[color=blue]
> 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 = '').[/color]

#v+

if not msg:
print 'msg is empty'

#v-

--
Klaus Alexander Seistrup
Magnetic Ink, Copenhagen, Denmark
http://magnetic-ink.dk/
  #3  
Old July 19th, 2005, 02:20 AM
Sean McIlroy
Guest
 
Posts: n/a
Default Re: Null String Variable

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

  #4  
Old July 19th, 2005, 02:20 AM
Peter Hansen
Guest
 
Posts: n/a
Default Re: Null String Variable

Sean McIlroy wrote:[color=blue]
> well, somebody's already pointed out that bool(msg)==False iff msg==''.[/color]

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.
[color=blue]
> i'm curious to know what's wrong with simply writing
>
> if msg=='':
> ## do_something[/color]

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
  #5  
Old July 19th, 2005, 02:21 AM
Steve Holden
Guest
 
Posts: n/a
Default Re: Null String Variable

Sean McIlroy wrote:[color=blue]
> 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
>[/color]
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/

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles