Sanchit
Sanchit

Reputation: 3289

How to string format a whole list?

I have a list/array and I want to string format it. For example,

 a = [1, 2, 3] and output that I want should be 010203

I tried with b = "%.2d %(a)", however, it gives me an error TypeError: %d format: a number is required, not list. Of course, I can understand this error, but, I don't know the other way.

The code should be generic, I mean, when my list is like a = [10, 11, 12, 13], then, it should print the output like 10111213.

Upvotes: 1

Views: 95

Answers (1)

alecxe
alecxe

Reputation: 473863

>>> a = [1,2,3]
>>> "".join("%.2d" % i for i in a)
'010203'
>>> a = [10, 11, 12, 13]
>>> "".join("%.2d" % i for i in a)
'10111213'

Upvotes: 2

Related Questions