Cecil Westerhof <ce...@decebal.nl> writes: > I have a file where I used a lot of {0}, {1} and {2}. Most but not all > are changed to {0:.3E}, {1:.3E} and {2:.3E}. But when I want to change > the format I come in dependency hell. > > I could do something like: > format = ':.3E' > fmt0 = '{0' + format + '} > fmt1 = '{1' + format + '} > fmt2 = '{2' + format + '} > > and replace occurrences of: > 'before {0} after' > with: > 'before ' + fmt0 + ' after' > > But that does not really make me happy. Is there a better way?
'Better' is often a bit tricky. You could always factor the formatted printing into a function, but you will have considered that. You could pass the format to used to the format function: format_for_numbers = '.3E' ... 'x = {0:{nfmt}}, y = {1:{nfmt}}'.format(3.4, 4.5, nfmt=format_for_numbers); or you could do that with just the width (if that is the variable part): 'x = {0:.{nwd}E}, y = {1:.{nwd}E}'.format(3.4, 4.5, nwd=4); -- Ben. -- https://mail.python.org/mailman/listinfo/python-list