string[i:j:k]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • konstantin

    string[i:j:k]

    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!
  • alex23

    #2
    Re: string[i:j:k]

    On Jul 22, 3:10 pm, konstantin <konstantin.sel iva...@gmail.co mwrote:
    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 = "ABCABCABCABCAB C"
    >>s[0:6:3]
    'AA'

    Hope this helps.

    - alex23

    Comment

    • konstantin

      #3
      Re: string[i:j:k]

      On Jul 22, 9:18 am, alex23 <wuwe...@gmail. comwrote:
      On Jul 22, 3:10 pm, konstantin <konstantin.sel iva...@gmail.co mwrote:
      >
      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 = "ABCABCABCABCAB C"
      >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?

      Comment

      • Gary Herron

        #4
        Re: string[i:j:k]

        konstantin wrote:
        On Jul 22, 9:18 am, alex23 <wuwe...@gmail. comwrote:
        >
        >On Jul 22, 3:10 pm, konstantin <konstantin.sel iva...@gmail.co mwrote:
        >>
        >>
        >>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 = "ABCABCABCABCAB C"
        >>>>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?
        >
        --

        >

        Comment

        • John McMonagle

          #5
          Re: string[i:j:k]

          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

          Comment

          • konstantin

            #6
            Re: string[i:j:k]

            Now it's clear.
            Thanks.

            Comment

            • Terry Reedy

              #7
              Re: string[i:j:k]



              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:

              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.

              Comment

              Working...