[Tutor] counting space-delimited strings within a sequence

John Purser johnp at HomeLumber.com
Fri Apr 2 16:48:42 EST 2004

Well this works:
>>> a = "How    do you 			do?"
>>> b = a.split()
>>> n = len(b)
>>> if not n:	#or if n == 0
... 	n = 1

-----Original Message-----
From: Vicki Stanfield [mailto:vicki at stanfield.net]
Sent: Friday, April 02, 2004 2:33 PM
To: tutor at python.org
Subject: [Tutor] counting space-delimited strings within a sequence

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:

        args = parameters.split(" ")
        for count in args:

        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.


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

Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list