1

If c = '32486784298',

then '{0}{1}{2}.{3}{4}{5}.{6}{7}{8}-{9}{10}'.format(*c)

prints '324.867.842-98'.

Is there a simplest way to do this? (with no def please)

Makoto
  • 104,088
  • 27
  • 192
  • 230
Ricardo Carmo
  • 643
  • 5
  • 6
  • 6
    What do you want to do? If your only goal is to run that exact line of code, that is probably the simplest way. Otherwise, what is the general thing you are trying to do? – murgatroid99 May 14 '12 at 19:24
  • This link might be helpfull: http://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators-in-python-2-x – BluePeppers May 14 '12 at 19:24

3 Answers3

3

In the latest versions of Python you can omit number in string-format placeholders:

>>> '{}{}{}.{}{}{}.{}{}{}-{}{}'.format(*c)
'324.867.842-98'

It works in Python 2.7.

ovgolovin
  • 13,063
  • 6
  • 47
  • 78
  • +1. But the problem is (both here and in the question) that it works only in very special situation when the string is of the exact length. – pepr May 14 '12 at 20:28
  • @pepr Yes. And I hope this is the case. Otherwise it will result in throwing exception. :) – ovgolovin May 14 '12 at 20:33
0
''.join([c[0:3],'.',c[3:6],'.',c[6:9],'-',c[9:11])

OR

c[0:3]+'.'+c[3:6]+'.'+c[6:9]+'-'+c[9:11]
Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • 1
    The join does not work for two reasons. 1) Right after ''.join( there is an unclosed [ character. 2) When the unclosed [ is removed, there is a compile error saying join takes one argument. – octopusgrabbus Jul 16 '12 at 00:49
-1
'%s.%s.%s-%s' % tuple(c[i:j] for i, j in ((0, 3), (3, 6), (6, 9), (9, 11)))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153