Number letter counts I'm trying to become more proficient in Python and have decided to run through the Project Euler problems. 

In any case, the problem that I'm on (17) wants me to count all the letters in the English words for each natural number up to 1,000. In other words, one + two would have 6 letters, and so on continuing that for all numbers to 1,000.

I wrote this code, which produces the correct answer (21,124). However, I'm wondering if there's a more pythonic way to do this.

I have a function (num2eng) which translates the given integer into English words but does not include the word "and".  

for i in range (1, COUNTTO + 1):
    container = num2eng(i)
    container = container.replace(' ', '')
    print container
    if i &gt; 100:
        if not i % 100 == 0:
            length = length + container.__len__()  + 3 # account for 'and' in numbers over 100
        else:
            length = length + container.__len__() 
    else:
        length = length + container.__len__() 
    print length


There is some repetition in my code and some nesting, both of which seem un-pythonic.

I'm specifically looking for ways to make the script shorter and with simpler loops; I think that's my biggest weakness in general.
Here are a few things:


container.__len__() – You should never call magic methods directly. Just use len(container).
As you don’t know to what amount you need to increment (i.e. your COUNTTO), it would make more sense to have a while loop that keeps iterating until you reach your final length result:

while length &lt; 1000:
     # do stuff with i
     length += # update length
     i += 1

You could also make use of itertools.count:

length = 0
for i in itertools.count(1):
    length += # update length
    if length &gt; 1000:
        break

not i % 100 == 0 – When you already have a operator (==) then don’t use not to invert the whole condition, but just use the inverted operator: i % 100 != 0
Also having additional counting logic outside of your num2eng does not seem to be appropriate. What does that function do exactly? Shouldn’t it rather produce a real number using a complete logic? Ideally, it should be as simple as this:

length = 0
for i in itertools.count(1):
    length += len(num2eng(i))
    if length &gt; 1000:
        break