简体   繁体   English

我可以在python中重复一个字符串格式描述符吗?

[英]Can I repeat a string format descriptor in python?

In fortran, I am able to repeat a format descriptor to save rewriting it many times, for example: 在fortran中,我能够重复格式描述符以保存多次重写,例如:

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? 可以在python中使用类似的方法吗?

For example, say I wanted to do the same in python, I would have to write: 例如,假设我想在python中执行相同的操作,我必须写:

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? 有没有办法重复格式描述符,比如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 格式字符串是普通字符串,因此可以将其乘以int

>>> '{:5d} '*5
'{:5d} {:5d} {:5d} {:5d} {:5d} '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM