[Tutor] counting space-delimited strings within a sequence

Vicki Stanfield vicki at stanfield.net
Fri Apr 2 16:33:28 EST 2004


This is probably a very simple question, but I am not sure of the best way
to handle it. I am attempting to determine the number of parameters that
are passed into my function. There might be none. I wrote the following:

        i=0
        args = parameters.split(" ")
        for count in args:
            i+=1

        self.numparams = i

but if there are no parameters, self.numparams is still set to 1 instead
of 0. I don't understand, but when I run this in idle, I get the same
result. How do I split up a string of words which are space-delimited and
handle the empty string as well?  And yes, I'm sure that I'm just missing
some fundamental Pythonism.

--vicki

"A pessimist sees the difficulty in every opportunity; an optimist sees
the opportunity in every difficulty."
  --  Winston Churchill




More information about the Tutor mailing list