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

  • Thread starter Lorenzo Thurman
  • Start date
L

Lorenzo Thurman

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
 
M

mensanator

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?
 
L

Lorenzo

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?

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 '<'
 
L

Lorenzo

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?

How would you get a slice of a[0] from your example? 'cde' for example?
 
G

Georg Brandl

Lorenzo said:
Do it correctly. Post your actual example that fails
and the related error message. Possibnly your indexes
were out of range.


Well, there wouldn't be a solution to a non-existent
problem, would there?

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
 
M

mensanator

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.
Well, there wouldn't be  a solution to a non-existent
problem, would there?

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
'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.
 
M

mensanator

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.
Well, there wouldn't be  a solution to a non-existent
problem, would there?

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).
 
L

Lorenzo

On Apr 8, 11:34?am, Lorenzo Thurman <[email protected]>
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?

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).


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.
 
L

Lorenzo

Georg Brandl said:
Lorenzo said:
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.
 
A

Alex Martelli

Lorenzo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top