How do I get a slice of a string held in a tuple?

mensanator at aol.com mensanator at aol.com
Sun Apr 8 14:22:16 EDT 2007


On Apr 8, 12:29�pm, Lorenzo <lore... at excitement.com> wrote:
> In article <1176050876.835238.68... at d57g2000hsg.googlegroups.com>,
>
>
>
>
>
>  "mensana... at aol.com" <mensana... at aol.com> wrote:
> > On Apr 8, 11:34?am, Lorenzo Thurman <lore... at diespammerhethurmans.com>
> > wrote:
> > > I have tuple which hold a string in tup[0]. I want to get a slice of
> > > that string. I thought I would do something like:
> > > tup[0][start:end]
> > > But this fails.
>
> > No, it doesn't.
>
> > >>> a = ('abcdefg','hijkl')
> > >>> a[0]
> > 'abcdefg'
> > >>> a[0][1:2]
> > 'b'
>
> > > How do I go about it?
>
> > Do it correctly. Post your actual example that fails
> > and the related error message. Possibnly your indexes
> > were out of range.
>
> > > I googled this and found a couple
> > > of references, but no solution.
>
> > Well, there wouldn't be  a solution to a non-existent
> > problem, would there?
>
> > > TIA
>
> How would you get a slice of a[0] from your example? 'cde' for example?

'b' _was_ a slice of a[0]. but for your specific example

>>> a[0][2:5]
'cde'

Keep in mind the "end" value is not returned and indexing
starts with 0, so "cde" represents the 3rd, 4th & 5th
characters making the slice 2:5.

>
> --
> "My Break-Dancing days are over, but there's always the Funky Chicken"
> --The Full Monty




More information about the Python-list mailing list