Please explain to me, why in the following code, in the first line of output, there is a sign ">" standing before "spam"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please explain to me, why in the following code, in the first line of output, there is a sign ">" standing before "spam"?

class SpecialString: def __init__(self, cont): self.cont = cont def __gt__(self, other): for index in range(len(other.cont)+1): result = other.cont[:index] + ">" + self.cont result += ">" + other.cont[index:] print(result) spam = SpecialString("spam") eggs = SpecialString("eggs") spam > eggs Output: >>> >spam>eggs e>spam>ggs eg>spam>gs egg>spam>s eggs>spam> >>>

16th Mar 2017, 1:15 AM
Sergio
Sergio - avatar
2 Answers
+ 2
Because the first iteration other.cont[:index] returns an empty string (given that index=0).
16th Mar 2017, 8:34 PM
Ivonne Ibarra
Ivonne Ibarra - avatar
+ 1
Now I got it. It is obvious. Sometimes something goes wrong in our brains :).
17th Mar 2017, 1:06 AM
Sergio
Sergio - avatar