Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: How to check if a string is empty in python?

Thread Tools

RE: How to check if a string is empty in python?

Hamilton, William
Posts: n/a
> -----Original Message-----
> From: Removed)
> On May 4, 5:02 am, Jaswant <(E-Mail Removed)> wrote:
> > This is a simple way to do it i think
> >
> > s=hello
> >
> > >>> if(len(s)==0):

> >
> > ... print "Empty"
> > ... else:
> > ... print s
> > ...
> > hello

> But you are still making the assumption that s is a string.
> (BTW, you need quotes around your example.)
> For example:
> >>> print a,b

> 11 11
> Can you tell which one is the string? I.e., which one had quotes
> around it?
> If you correctly assume that it was b, then yes, your example works.
> >>> print len(b)

> 2
> If you incorrectly assume it was a, then the example doesn't work.
> >>> print len(a)

> Traceback (most recent call last):
> File "<pyshell#8>", line 1, in <module>
> print len(a)
> TypeError: object of type 'int' has no len()
> You have to know that a variable is a string before you try to do a
> len().
> Dynamic typing is a feature, but that doesn't relieve you of the
> necessary tests.

Your point would be important if the question were "How can I tell if x
is an empty string?" On the other hand, "How to check if a string is
empty?" implies that the OP already knows it is a string. Maybe he's
been using string methods on it, maybe he got it from a function that he
knows provides a string. Maybe he's checked its type. It doesn't really
matter, if he's aware it is a string he doesn't have to test it for

-Bill Hamilton
Reply With Quote

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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Empty gridview cells and checking for empty string Savvoulidis Iordanis ASP .Net 1 09-05-2008 06:15 AM
behavior varied between empty string '' and empty list [] Tzury Bar Yochay Python 1 03-24-2008 06:56 PM
check if string contains numeric, and check string length of numeric value C++ 5 06-30-2005 01:08 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM