3 number and dot..

elf lucian.cancescu at gmail.com
Wed Oct 31 17:06:36 EDT 2007


On Oct 31, 9:58 pm, Abandoned <best... at gmail.com> wrote:
> Hi..
> I want to do this:
> for examle:
> 12332321 ==> 12.332.321
>
> How can i do?

If you want do define your own function this will work, no matter how
long the number is, or what separator you choose:

def conv(s, sep='.'):
    start=len(s)%3
    last=start
    result=s[0:start]
    for i in range(start+1,len(s)):
        if (i-start)%3==0:
            if last==0:
                result+=s[last:i]
            else:
                result+=sep+(s[last:i])
            last=i

    if last==len(s) or last==0:
        result+=s[last:len(s)]
    else:
        result+=sep+s[last:len(s)]
    return result

print conv('1234567890000000')
print conv('1')
print conv('123')
print conv('1234')

>>>
1.234.567.890.000.000
1
123
1.234




More information about the Python-list mailing list