Strings for a newbie
Malcolm Wooden
mwooden at dtptypes.com
Fri May 27 09:23:19 EDT 2005
Yes Sergei, as 3 of the lines are Dim statements, the real code is just 4
lines, a totally logical. It's not the amout of code thats a probelm, it's
following the logic and structure thats important. As I said Python.. UGH!
Malcolm
"Sergei Organov" <osv at javad.ru> wrote in message
news:d7730r$qrb$1 at n6.co.ru...
> "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:
>
> Simple?! 7 lines of code for such a trivial task is 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.
>>
>> Now can I see how this is done in Python? - nope!
>
> from string import split
> s = "This is a sentence of words"
> a = split(s)
>
> or,
>
> s = "This is a sentence of words"
> a = s.split()
>
> or even
>
> a = "This is a sentence of words".split()
>
> --
> Sergei.
More information about the Python-list
mailing list