[Beds] python question
Lee Larcombe
lee at larcombe.uk-internet.org.uk
Tue Mar 22 21:19:08 GMT 2005
Hi
Thanks for your solution. Steve (Stephen Newey) also supplied an answer
that you might be interested in - below
\*quoted message pasted below*\
A string in Python is an immutable list, so you can treat it just like
one, eg:
for char in string:
print char
Or...
print string[1:3]
To make it a regular list you could...
array = []
for char in string: array.append(char)
I'm pretty sure there's a better way of doing that, just don't recall
it right now :)
Hope that helps
Steve
On Tuesday, March 22, 2005, at 08:48 pm, Darren Parkinson wrote:
>
> I think you can do this in the following way:
>
> string = 'abcde'
> items = [ word[0] for word in string ]
> print items
> print items[0]
>
> Is this what you mean? I've just downloaded it too so not sure if this
> is the right answer, but it seems to work... Certainly prints ['a',
> 'b', 'c', 'd', 'e'] and a respectively... Would seem that this is
> unexpected functionality since this is supposed to take the first
> letter
> of each word... (I was pretty lucky with google :))
>
> Regards,
> Darren.
>
>
> -----Original Message-----
> From: Lee Larcombe [mailto:lee at larcombe.uk-internet.org.uk]
> Sent: 22 March 2005 20:13
> To: beds at mailman.lug.org.uk
> Subject: [Beds] python question
>
>
> Hi
>
> I am trying to learn python, and i have hit a problem I can't find a
> solution for.
>
> I want to split a string into a list - ie
>
> string = 'abcde'
>
> would become
>
> list = ['a', 'b', 'c', 'd', 'e']
>
> I can do this in perl by using:
>
> @array= unpack("A1" x length($string), $string);
>
> Can anyone tell me a python way of doing this???
>
> I know python could use string.split(";") to split a string on a
> delimiter (; in this case) but there doesn't seem a way to use split
> without a delimiting character
>
> I can't believe this is impossible - please help!!!
>
> Lee.
>
>
> _______________________________________________
> Beds mailing list
> Beds at mailman.lug.org.uk http://mailman.lug.org.uk/mailman/listinfo/beds
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>
> _______________________________________________
> Beds mailing list
> Beds at mailman.lug.org.uk
> http://mailman.lug.org.uk/mailman/listinfo/beds
>
More information about the Beds
mailing list