Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Null String Variable

Reply
Thread Tools

Null String Variable

 
 
rotary
Guest
Posts: n/a
 
      05-16-2005
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


 
Reply With Quote
 
 
 
 
Klaus Alexander Seistrup
Guest
Posts: n/a
 
      05-16-2005
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/
 
Reply With Quote
 
 
 
 
Sean McIlroy
Guest
Posts: n/a
 
      05-16-2005
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

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      05-16-2005
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
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      05-17-2005
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/

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
transforming from a non-null terminated string into a null terminatedstring ssylee C Programming 4 08-12-2008 09:57 PM
How to tell if a string element is null or really null? Jiho Han ASP .Net 13 07-29-2006 01:45 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
interpreting a null pointer as an empty (null string) Dennis Allison C Programming 11 03-06-2004 12:47 PM



Advertisments