Possible to slice a string with unpacked tuple?

Benjamin Kaplan benjamin.kaplan at case.edu
Sat Jan 24 17:47:43 EST 2009


On Sat, Jan 24, 2009 at 5:31 PM, <python at bdurham.com> wrote:

>  Is there a way to slice a string with a tuple without unpacking the tuple?
>
> >>> myString = "111-222-333-444"
> >>> myString[ 4: 7 ]
> '222'
>
> Is there some way I could perform an identical slicing operation with a
> tuple like ( 4, 7 ) without having to unpack the tuple?
>
> >>> myTuple = ( 4, 7 )
>
> Thanks!
> Malcolm
>

1) What's wrong with unpacking the tuple?

2) Use the built-in slice.

>>> myString = "111-222-333-444"
>>> myTuple = (4,7)
>>> mySlice = slice(*myTuple)
>>> myString[mySlice]
'222'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090124/28ee2cd7/attachment-0001.html>


More information about the Python-list mailing list