3 number and dot..

Paul McNett p at ulmcnett.com
Wed Oct 31 17:41:25 EDT 2007


Abandoned wrote:

> On Oct 31, 10:18 pm, Paul McNett <p... at ulmcnett.com> wrote:
>> Abandoned wrote:
>> > Hi..
>> > I want to do this:
>> > for examle:
>> > 12332321 ==> 12.332.321
>>
>> > How can i do?
>>
>> Assuming that the dots are always in the 3rd and 7th position in the string:
>>
>> def conv(s, sep="."):
>>    l = [s[0:3], s[3:6], s[6:]]
>>    return sep.join(l)
>>
>> print conv("12332321")


> But it's starts from the end..
> print conv("12332321")
> 123.323.21
> This is wrong it would be 12.332.321

Ok, my slicing was off by one. Mea culpa. I leave it as an exercise to 
fix it. It is really easy, if you understand basic slicing in Python. If 
you don't understand slicing, you should google 'python slicing tutorial'.

But, from the other messages in the thread it has become apparent that 
you are wanting to group by thousands (that wasn't at all clear in your 
initial message).

Chris Mellon has given you the best response: use the locale module for 
this. Other people have done all the work for you, all you need to do is 
learn how to use locale to get what you want.

It seems like you are looking for a spoonfed solution rather than 
looking for guidance on how to solve the problem for yourself. If I'm 
wrong about that assessment, I apologize in advance.

Best regards and good luck with your project!
Paul

-- 
pkm ~ http://paulmcnett.com



More information about the Python-list mailing list