[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:

        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


_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list