Ad
  • Custom User Avatar

    I believe len() is not calculated when you ask for it, but when the string changes. Don't change the string and ther is no charge. Strings are immutable, therefore str(value) will have the same id() no matter where you use it. As both loops are executed the same number of times, they will have the same number of conversions to int. The for loop may be slightly faster, and possibly easier to read.

    Someone please correct me if I'm wrong!

  • Custom User Avatar

    It is not optimally efficient, sure. But not O(n^2) - string gets shorter on each iteration.