Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

algotruneman

  • 3 years ago

I'm trying to get a handle on string slicing. Thanks for any help. I thought -1 referenced the last character of a string. See my Gist for the code snip. https://gist.github.com/4328274 Further reading shows that print group[start: ] is the right format. The question still remains in my mind, though.

  • This Question is Closed
  1. exchaoordo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I can't give you the deep theory, but if you try this, you'll see what's going on: x = "012345" print x[:-1] ##up to but not including last character print x[-1]##print last character print x[0:] ##up to and including last character

  2. algotruneman
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks, exchaoordo. I guess my problem is that the logic of -1 being both a reference to the last character of the string AND the penultimate (last - 1) character is confusing. But, I'll live with it and move on.

  3. exchaoordo
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I share your pain: I really want things to actually make sense rather than just knowing how to make stuff work.

  4. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy