Strings for a newbie

Warren Block wblock at wonkity.com
Sat May 28 23:02:11 EDT 2005


James Hofmann <jhofmann at ucsc.edu> wrote:
> Malcolm Wooden <mwooden <at> dtptypes.com> writes:
>> 
>> I'm trying to get my head around Python but seem to be failing miserably. I 
>> use RealBasic on a Mac and find it an absolute dream! But Python....UGH!
>> 
>> I want to put a sentence of words into an array, eg "This is a sentence of 
>> words"
>> 
>> In RB it would be simple:
>> 
>> Dim s as string
>> Dim a(-1) as string
>> Dim i as integer
>> 
>> s = "This is a sentence of words"
>> For i = 1 to CountFields(s," ")
>>   a.append NthField(s," ",i)
>> next
>> 
>> That's it an array a() containing the words of the sentence.

[snip]

> Now a "slim" version:
>
> s = "This is a sentence of words"
> print s.split()

To match the original program, it should be:

s = "This is a sentence of words"
a = s.split()

...assigning the list returned by split() to a variable called a.

-- 
Warren Block * Rapid City, South Dakota * USA



More information about the Python-list mailing list