Stupid string.split question

Andrew Dalke adalke at mindspring.com
Wed Aug 6 16:00:13 EDT 2003


Brian Kelley:
> "ABCDEF".split("") shouldn't equal ["A", "B", "C", "D", "E", "F"]?

(which, btw, raises a ValueError.)

Hmm.  Perl does that, right?  I see your point about the symmetry,

> It seems that if you can join with an empty seperator, you should be
> able to split with one.

Even better, newer splits take a substring to split on, as in

>>> "ABCDEFG".split("BCD")
['A', 'EFG']
>>> "ABCDEFG".split("DCE")
['ABCDEFG']
>>>

arguing for a closer similarity.

> I suppose the pythonic way is [x for x in "ABCDEF"] which doesn't make
> as much sense to be.

list("ABCDEF")

[to reverse a string]
> l = [x for x in forward_string]
> l.reverse()
> reversed_string = "".join(l)

or in Python 2.3
>>> "ABCDEF"[::-1]
'FEDCBA'
>>>

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list