Null String Variable

Discussion in 'Python' started by rotary, May 16, 2005.

  1. rotary

    rotary Guest

    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
    rotary, May 16, 2005
    #1
    1. Advertising

  2. 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/
    Klaus Alexander Seistrup, May 16, 2005
    #2
    1. Advertising

  3. rotary

    Sean McIlroy Guest

    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
    Sean McIlroy, May 16, 2005
    #3
  4. rotary

    Peter Hansen Guest

    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
    Peter Hansen, May 17, 2005
    #4
  5. rotary

    Steve Holden Guest

    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/
    Steve Holden, May 17, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    7,317
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,588
    Mike Schilling
    Mar 29, 2006
  3. Dennis Allison

    interpreting a null pointer as an empty (null string)

    Dennis Allison, Mar 5, 2004, in forum: C Programming
    Replies:
    11
    Views:
    808
    Derk Gwen
    Mar 6, 2004
  4. Jiho Han
    Replies:
    13
    Views:
    631
    John Saunders
    Jul 29, 2006
  5. ssylee
    Replies:
    4
    Views:
    501
    CBFalconer
    Aug 12, 2008
Loading...

Share This Page