0

Question Given a string, return a new string made of 3 copies of the last 2 chars of the original string. The string length will be at least 2.

This is what I did, but it didn't return true on every case.

def extra_end(str):
    return str[2:]*3

So I checked the solution and found this answer:

def extra_end(str):
    return str[-2:]*3

My question is what's the difference between [-2:] and [2:] ?

Faibbus
  • 1,115
  • 10
  • 18
  • 1
    They are drastically different. Have you tried to see what your implementation does? – OneRaynyDay Apr 26 '18 at 14:53
  • Also see https://stackoverflow.com/questions/11367902/negative-list-index. It's for lists, but this also applies to strings. – rayryeng Apr 26 '18 at 14:53
  • Off-topic, but since `str` is the python type for strings, you'd be better off naming your parameter differently (original_string maybe). – Faibbus Apr 26 '18 at 14:55

0 Answers0