Single string vs list of strings

Grant Edwards invalid at invalid
Thu Oct 30 10:55:05 EDT 2008


On 2008-10-30, Scott Sharkey <ssharkey at linuxunlimited.com> wrote:

> I have a need to determine whether a passed variable is a single string, 
> or a list of strings.  What is the most pythonic way to do this?

>>> type('asdf') is list
False
>>> type(['asdf','qwer']) is list
True

The question you might want to asked is whether the parameter
is a single string or a sequence of strings.  That way your
code will also work with an iterator that returns strings.

>>> type('asdf') is str   
True

Checking to see if something is a sequence of strings is a bit
trickier, since a string is actually a sequence of strings.
You first have to verify that it's not a string, and then check
for the API that you're going to use (iteration or indexing).

-- 
Grant Edwards                   grante             Yow! I represent a
                                  at               sardine!!
                               visi.com            



More information about the Python-list mailing list