By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,481 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,481 IT Pros & Developers. It's quick & easy.

string[i:j:k]

P: n/a
Hello,
I'm not a newbie in python, but recently faced a problem in simple
expression:
some_string[i:j:k]
What does it mean? I believe this grammar (http://docs.python.org/ref/
slicings.html) describes the syntax. But I can't grasp it.
Thanks!
Jul 22 '08 #1
Share this Question
Share on Google+
6 Replies

P: n/a
On Jul 22, 3:10*pm, konstantin <konstantin.seliva...@gmail.comwrote:
some_string[i:j:k]
What does it mean?
i = start position, j = end position, k = step size
>>s = "ABABABABABABAB"
s[0:6:2]
'AAA'
>>s = "ABCABCABCABCABC"
s[0:6:3]
'AA'

Hope this helps.

- alex23
Jul 22 '08 #2

P: n/a
On Jul 22, 9:18 am, alex23 <wuwe...@gmail.comwrote:
On Jul 22, 3:10 pm, konstantin <konstantin.seliva...@gmail.comwrote:
some_string[i:j:k]
What does it mean?

i = start position, j = end position, k = step size
>s = "ABABABABABABAB"
s[0:6:2]
'AAA'
>s = "ABCABCABCABCABC"
s[0:6:3]

'AA'

Hope this helps.

- alex23
Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>s = '123456789'
s[::-2]
'97531'

but
>>s[:-1:-2]
''
though I expected something like '8642'
What did i missed?

Jul 22 '08 #3

P: n/a
konstantin wrote:
On Jul 22, 9:18 am, alex23 <wuwe...@gmail.comwrote:
>On Jul 22, 3:10 pm, konstantin <konstantin.seliva...@gmail.comwrote:

>>some_string[i:j:k]
What does it mean?
i = start position, j = end position, k = step size

>>>>s = "ABABABABABABAB"
s[0:6:2]
>
'AAA'
>>>>s = "ABCABCABCABCABC"
s[0:6:3]
>
'AA'

Hope this helps.

- alex23

Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>>s = '123456789'
s[::-2]
'97531'

but
>>>s[:-1:-2]
The slice s[:-1]
means start at zero and go to n-1(where n-len(s))
(it does not mean start at zero and go to -1)

So since the indexing is counting upward, the step size had better be
positive. Thus:
>>s = '123456789'
s[:-1:2]
'1357'
>>>

Gary Herron
''
though I expected something like '8642'
What did i missed?

--
http://mail.python.org/mailman/listinfo/python-list
Jul 22 '08 #4

P: n/a
konstantin wrote:
>
Thanks!
It seems that negative step leads in reverse direction.
But logic isn't completely clear for me.
>>>s = '123456789'
s[::-2]
'97531'

but
>>>s[:-1:-2]
''
though I expected something like '8642'
What did i missed?

--

You need to *start* at the second from last index:

s[-2::-2]

Regards,

John
Jul 22 '08 #5

P: n/a
Now it's clear.
Thanks.
Jul 22 '08 #6

P: n/a


konstantin wrote:
Hello,
I'm not a newbie in python, but recently faced a problem in simple
expression:
some_string[i:j:k]
What does it mean? I believe this grammar (http://docs.python.org/ref/
slicings.html) describes the syntax. But I can't grasp it.
When you post a link, please put it on one line by itself, like this:
http://docs.python.org/ref/slicings.html
so it will be clickable as a whole by modern newsreaders.

Experimenting with bits of code in the interactive interpreter (or IDLE,
which is easier to cut from), like the responders did, is a great way to
learn.

Jul 22 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.