Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is fortran-like print in python possible?

Tags:

python

fortran

is it possible some way to "print" in python in a fortran like way like this?

1     4.5656
2    24.0900
3   698.2300
4    -3.5000

So the decimal points is always in the same column, and we get always 3 or n decimal numbers?

Thanks

like image 491
Open the way Avatar asked Feb 25 '10 16:02

Open the way


2 Answers

>>> '%11.4f' % -3.5
'    -3.5000'

or the new style formatting:

>>> '{:11.4f}'.format(-3.5)
'    -3.5000'

more about format specifiers in the docs.

like image 155
SilentGhost Avatar answered Oct 22 '22 18:10

SilentGhost


You could also take a look at the fortranformat library on PyPI or the project page if you wanted to fully recreate FORTRAN text IO.

If you have any questions, send me an email (I wrote it).

like image 24
Brendan Avatar answered Oct 22 '22 18:10

Brendan