Jim and Laura Ahl said unto the world upon 2005-04-14 12:08:

<SNIP>

'my test string'[-1:-4:-1]
'gni'

When I do this it tells me that the sequence index must be an integer. What is that telling me and how do I fix that? Jim


Kent addressed that already. But, my mistake for not thinking about
which versions of Python my examples required.

<SNIP>

We can also do
'my test string'[-4:]
'ring'

Ok, I agree with what this is doing. So if I type in print i[-1] this gives me that last letter of the string. But when I type in print i[-1:] it does nothing, because there is nothing after -1. So
I need to find some charecter that takes the string in the opposite direction. like print i[-1:-4] but that does not print anything (why)?

>>> '[-1:] does give something!'[-1:] '!' >>>

Again (from my last post on the thread):

This tells Python to start at the end of the string and go *forward*, one position at a time, up to, but not including, the -4
position. But, since there is nothing forward from the end of the
string, this gives the empty string.

I don't know how to say that more clearly :-) Perhaps you could say
more about why it doesn't help? (It is also possible you weren't asking again -- your email client isn't putting in quote marks so it is hard to be sure, and I've deleted the original email. If you weren't asking again; never mind ;-)



When I put another number behind this it gives me it must be an integer print i[-1:-4:-1] so what have I got messed up and why
can't this work?

Again, see Kent's post.


And if an iputted word or string in typed in how do you tell it to
go to the end of it and stop?  I assumed that print i[-1:]  would
have done it

Go to the end from where? >>> 'a string'[-1:] # "start at the last character, and go forward." 'g' >>> 'a string'[3:] # "start at the 4th character, and go forward." 'tring' >>> 'a string'[0:] # "start at the 1st character, and go forward." 'a string' >>>

([3:] gives 4th on, as indicies start at 0; thus [0:] gives the whole string.)

<SNIP>

(from my last post on the thread):
[*] slots? I mean: 'I am indexed with 1 slot'[4] 'I am indexed with
 2 slots'[4:6] 'I am indexed with 3 slots'[4:6:1] 'None of my slots
 have been "specified" '[:]

(There must be a better term that `slots', but it is 3am :-)

Some one wrote me off list to mention I was looking for "index parameters". Thanks, anonymous donor!


Best to all,

Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to