1

May be a question that has already been asked: How can I print an integer in a user definied format.

For example

input = 123456
print(".....".format(input))

where the output should be:

"ab123.45.6"

Unfortunately, slicing in string formating is not possible. So something like:

"ab{0[:2]}.{0[3:4]}.{0[5]}".format(str(input))

would not work. But is there a better solution than:

"ab{0[0]}{0[1]}{0[2]}.{0[3]}{0[4]}.{0[5]}".format(str(input))
RaJa
  • 1,415
  • 12
  • 16

1 Answers1

1

Why not make the slices format() arguments? It looks a bit cleaner.

user_input = str(123456)
print("ab{0}.{1}.{2}".format(user_input[:3], user_input[3:5], user_input[5]))
Dušan Maďar
  • 8,437
  • 4
  • 39
  • 62
  • That is in fact a cleaner solution, but still rather clumsy. I would prefer something like "AB####.##" in Excel. – RaJa Jul 29 '16 at 11:12