0
Python Expressions
s = "bananab" while s[0] == s[-1]: mid = len(s)//2 s = s[1:mid] + s[0] + s[mid:-1] print (s) WHY is output "bnaa" instead of "anbana"? I tried to print (s[1:mid]) , (s[0]) and (s[mid:-1]) seperately and it supports my theory that anbana should be the output. Thank you for your time.
2 Antworten
+ 3
If your condition is s[0] == s[-1] then it will continue for
(Capitals are used to show equality)
BananaB --> condition met
AnbanA --> condition met
NbaaN --> condition met
Bnaa --> Here the loop will break as s[0] != [-1] so it is the output!
+ 2
I'll keep this really short. Just add a print(s) statement inside the while loop and you'll know.