467,099 Members | 1,103 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,099 developers. It's quick & easy.

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

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. How do I go about it? I googled this and found a couple
of references, but no solution.
TIA
Apr 8 '07 #1
  • viewed: 1046
Share:
9 Replies
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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

Apr 8 '07 #2
In article <11*********************@d57g2000hsg.googlegroups. com>,
"me********@aol.com" <me********@aol.comwrote:
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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
Here's the code:

elapsedTime = mydata[1]
index = elapsedTime.find("real")
# the index will have a value 0f 110
totaltime = elapsedTime[index:]
# instead of this returning a shortened html string, i only
# get the left angle bracket '<'

--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Apr 8 '07 #3
In article <11*********************@d57g2000hsg.googlegroups. com>,
"me********@aol.com" <me********@aol.comwrote:
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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?

--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Apr 8 '07 #4
Lorenzo schrieb:
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

Here's the code:

elapsedTime = mydata[1]
index = elapsedTime.find("real")
# the index will have a value 0f 110
totaltime = elapsedTime[index:]
# instead of this returning a shortened html string, i only
# get the left angle bracket '<'
May it be that mydata[1] doesn't contain "real" at all? In that case,
find() returns -1, and the slice elapsedTime[-1:] always contains
at most one character.

If you replace "find" by "index", you get a ValueError exception if
"real" was not found, if that helps you.

Whenever one calls str.find(), one has to check the return value for -1.

Georg

Apr 8 '07 #5
On Apr 8, 12:29�pm, Lorenzo <lore...@excitement.comwrote:
In article <1176050876.835238.68...@d57g2000hsg.googlegroups. com>,

*"mensana...@aol.com" <mensana...@aol.comwrote:
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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
Apr 8 '07 #6
On Apr 8, 12:29�pm, Lorenzo <lore...@excitement.comwrote:
In article <1176050876.835238.68...@d57g2000hsg.googlegroups. com>,

*"mensana...@aol.com" <mensana...@aol.comwrote:
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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

Here's the code:

*elapsedTime = mydata[1]
*index = elapsedTime.find("real")
*# the index will have a value 0f 110
*totaltime = elapsedTime[index:]
*# instead of this returning a shortened html string, i only
*# get the left angle bracket '<'
This implies that '<' is the 111th character (counting
from 0) and that it is the last character since you used
[index:].

Print out the entire string elapsedTime, count from
0 to the characters you want and see if you have the
correct index numbers (verify them).

>
--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Apr 8 '07 #7
In article <11*********************@o5g2000hsb.googlegroups.c om>,
"me********@aol.com" <me********@aol.comwrote:
On Apr 8, 12:29�pm, Lorenzo <lore...@excitement.comwrote:
In article <1176050876.835238.68...@d57g2000hsg.googlegroups. com>,

*"mensana...@aol.com" <mensana...@aol.comwrote:
On Apr 8, 11:34?am, Lorenzo Thurman <lore...@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
Here's the code:

*elapsedTime = mydata[1]
*index = elapsedTime.find("real")
*# the index will have a value 0f 110
*totaltime = elapsedTime[index:]
*# instead of this returning a shortened html string, i only
*# get the left angle bracket '<'

This implies that '<' is the 111th character (counting
from 0) and that it is the last character since you used
[index:].

Print out the entire string elapsedTime, count from
0 to the characters you want and see if you have the
correct index numbers (verify them).


--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Oops! I sent the wrong piece of code. The above is actually the work
around which actually works. The bad code is this:
index = mydata[0].find("real")
elapsedTime = mydata[0][index:]

My apologies, but this is what fails.

--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Apr 9 '07 #8
In article <ma***************************************@python. org>,
Georg Brandl <g.******@gmx.netwrote:
Lorenzo schrieb:
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
Here's the code:

elapsedTime = mydata[1]
index = elapsedTime.find("real")
# the index will have a value 0f 110
totaltime = elapsedTime[index:]
# instead of this returning a shortened html string, i only
# get the left angle bracket '<'

May it be that mydata[1] doesn't contain "real" at all? In that case,
find() returns -1, and the slice elapsedTime[-1:] always contains
at most one character.

If you replace "find" by "index", you get a ValueError exception if
"real" was not found, if that helps you.

Whenever one calls str.find(), one has to check the return value for -1.

Georg
Oops! I sent the wrong piece of code. The above is actually the work
around which actually works. The bad code is this:
index = mydata[0].find("real")
elapsedTime = mydata[0][index:]

My apologies, but this is what fails.

--
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty
Apr 9 '07 #9
Lorenzo <lo*****@excitement.comwrote:
...
elapsedTime = mydata[1]
index = elapsedTime.find("real")
# the index will have a value 0f 110
totaltime = elapsedTime[index:]
...
Oops! I sent the wrong piece of code. The above is actually the work
around which actually works. The bad code is this:
index = mydata[0].find("real")
elapsedTime = mydata[0][index:]
The only difference is that in the "workaround that works" you're
mungling mydata[1], in the "bad code" mydata[0]. If you print or
otherwise emit the value of index in each case, I think the cause of
your problem will become clear (and it will probably be a value of index
that's -1 for the "failing" case, and >= 0 for the "working" case, as
other posts on this thread have already suggested).

No connection can exist between your problem, and the string being "held
in a tuple" or held in any other fashion.
Alex
Apr 9 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Roberto A. F. De Almeida | last post: by
108 posts views Thread by Bryan Olson | last post: by
40 posts views Thread by Ron Adam | last post: by
23 posts views Thread by Antoon Pardon | last post: by
7 posts views Thread by Alexandre Guimond | last post: by
2 posts views Thread by smichr@gmail.com | last post: by
3 posts views Thread by Bas | last post: by
2 posts views Thread by ajcppmod@gmail.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.