Create list from string

Ben Sizer kylotan at gmail.com
Fri Jun 13 16:27:57 CEST 2008


On Jun 13, 3:15 pm, ericdaniel <eric.acev... at gmail.com> wrote:
> Hi,
>
> I'm new to Python and I need to do the following:
>
> from this:   s = "978654321"
> to this :      ["978", "654", "321"]

What are your criteria for splitting this string? Every 3 characters?
If there isn't an even multiple of 3, which group should be shorter,
the first, the last, or maybe some other?

And do you even really need this as a string at all? Perhaps you
really just wanted to format the output of an integer? (I think that
may be done via the locale, but I am not sure.)

Often it's best to specify why you want to do something, as when using
a new language there is often a better way to achieve what you want
than the first way that occurs to you.

--
Ben Sizer



More information about the Python-list mailing list