A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

Regarding strings in Python, we know some_string[0] is the index of the first character of string some_string. Why is some_string[-1] the index of the last character instead of some_string[-0]? Would python evaluate -0 to 0 anyway?

  • This Question is Open
  1. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Intuitively, 0 is the first element in the string. So -1 is the one before it, or in other words, the last element, cause it's wrapping. In other words, the negative sign doesn't signify subtraction. it just signifies the value of the number inside the brackets. so -0 = 0 and -1 = -1 and 1=1.

  2. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I think you got confused cause you thought the -0 minus one less than 0. but it doesn't, hope that helps.

  3. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    to do that, i believe you'd use the actual subtract operator, like --. not just one -. Make sense?

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    \(\color{blue}{\text{Originally Posted by}}\) @ratinthecat Would python evaluate -0 to 0 anyway? \(\color{blue}{\text{End of Quote}}\) This is one reason, but there is another reason though. Here is one way to think of python indexing for negative values: `some_string[i] == some_string[len(some_string) + i]` Since the `len(...) - 1` gives the last index, the `-1` gives the last element.

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    These explanations really help. Thanks Wio and Curry!

  6. 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

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.