Can I repeat a string format descriptor in python?
Question:
In fortran, I am able to repeat a format descriptor to save rewriting it many times, for example:
write(*,'(i5,i5,i5,i5,i5)')a,b,c,d,e
could be rewritten as
write(*,'(5(i5))')a,b,c,d,e
Can a similar approach be used in python?
For example, say I wanted to do the same in python, I would have to write:
print "{0:5d} {1:5d} {2:5d} {3:5d} {4:5d}".format(a,b,c,d,e)
Is there some way to repeat the format descriptor, like in fortran?
Answers:
You can repeat the formatting string itself:
print ('{:5d} '*5).format(*values)
Format string is a normal string, so you can multiply it by int
>>> '{:5d} '*5
'{:5d} {:5d} {:5d} {:5d} {:5d} '
In fortran, I am able to repeat a format descriptor to save rewriting it many times, for example:
write(*,'(i5,i5,i5,i5,i5)')a,b,c,d,e
could be rewritten as
write(*,'(5(i5))')a,b,c,d,e
Can a similar approach be used in python?
For example, say I wanted to do the same in python, I would have to write:
print "{0:5d} {1:5d} {2:5d} {3:5d} {4:5d}".format(a,b,c,d,e)
Is there some way to repeat the format descriptor, like in fortran?
You can repeat the formatting string itself:
print ('{:5d} '*5).format(*values)
Format string is a normal string, so you can multiply it by int
>>> '{:5d} '*5
'{:5d} {:5d} {:5d} {:5d} {:5d} '