473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
9 1194
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
23
by: Antoon Pardon | last post by:
Now slices are objects in python, I was wondering if slice notation will be usable outside subscribtion in the future. Will it ever be possible to write things like: a = 4:9 for key, value in...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
2
by: smichr | last post by:
It seems to me that the indices() method for slices is could be improved. Right now it gives back concrete indices for a range of length n. That is, it does not return any None values. Using an...
3
by: Bas | last post by:
Hi, stupid question, but would it be possible to somehow merge xrange (which is supposed to replace range in py3k) and slice? Both have very similar start, stop and step arguments and both are...
2
by: ajcppmod | last post by:
I'm really confused about results of slices with negative strides. For example I would have then thought of the contents of mystr as: indices 0 1 2 3 4 5 6 7 8 content m y s t r i n...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.