I am trying to find an easy way to add a dot . to each 3 digit as a string ('43434' -> '43.434) and I found an interesting use of join for this task:
num = '1234567'
new_num = '.'.join(num[i:i+3] for i in range(0, len(num), 3))
# new_num = 123.456.7'
Well its close to what I want. This is what I want:
(its need to be `'1.234.567'`)
Why does it work like that? When I use slicing on join, it adds the addition for each item:
num = '2333'
>>> '.'.join(num[0:2])
<<< '2.3'
>>> '.'.join(num[0:3])
<<< '2.3.3'
I read somewhere that its called string as iterable concept. Can someone help me understand it?
num[i:i+3] for i in range(0, len(num), 3)- This is a generator expression. A quick Google search didn't show any good beginner introductions to generators, so I've linked the PEP proposing their introduction to the language.