Equivalent of str function (from python) in Fortran

In python str() method is used to convert a value (integer or real) to a string. Is it possible in Fortran? Apparently, it seems there is no such intrinsic procedure in Fortran. Any suggestion/s will be helpful.

4 Likes

You can create a function that uses an internal write to convert a real or integer to a string:

module m
implicit none
contains
!
pure function real_to_string(x) result(s)
real, intent(in) :: x
character (len=:), allocatable :: s
character (len=100) :: str
character (len=*), parameter :: fmt = "(f0.6)" ! choose the desired format
write (str,fmt) x ! convert x to fixed-length string
s = trim(str) ! create a string with no trailing spaces
end function real_to_string
!
end module m
!
program main
use m
implicit none
print "(a)", "s = '" // real_to_string(3.4) // "'"
end program main
! output:
! s = '3.400000'

Yeah, that’s a common function that I also missed. Here is an implementation:

Update: just use to_string from stdlib (see below).

2 Likes

There is to_string in the standard library. It works with integer, real, complex, and logical inputs.

4 Likes

thanks, @Beliavsky, @certik, and @milancurcic for the reply. That is what I needed.

Yes, the to_string in stdlib is the way to go. (I wasn’t sure if we had it.)

1 Like

You might be interested in the str function in the fpm package M_msg as well. Go to

M_msg

and select the “str” function for a description. It uses internal writes as well, but allows up to twenty intrinsic scalars, which I find very useful for creating error messages with values in them in particular. So it is basically the same as the functions previously listed when used with one variable, but has the added capability of taking multiple numeric as well as character arguments; making it trivial to create messages that need passed to an error handling function (ie. when you want to build messages to pass to another procedure instead of simply writing them with a WRITE statement), which is done with many libraries since LAPACK/BLAS at least.

2 Likes

@urbanjost’s str function is used as:

str('doubleprecision :',huge(0.0d0),0.0d0,12345.6789d0,tiny(0.0d0) )

While mine would be used as:

'doubleprecision :' // str(huge(0.0d0)) // str(0.0d0) // str(12345.6789d0) // str(tiny(0.0d0))

I do not believe your function defaults to padding with a space, so to make those equivalent would be

'doubleprecision: '//str(huge(0.0d0))//' '//str(0.0d0)//' '//str(12345.6789)//' '//str(12345.6789d0)//' '//str(tiny(0.0d0))

the delimiter is an optional parameter on the multi-parameter version but defaults to a space. My example is partly based on a unit test, so in both cases actual usage would probably look more like

str('<ERROR> A=',A,'is out of range and must be between',B,'and',C,'inclusive')

'<ERROR> A= '//str(A)//' is out of range and must be between '//str(B)//' and  '//str(C)//' inclusive'
1 Like

thanks.
Just for the information, fString also exists, and is an object-oriented library providing string objects.

Good catch, I forgot about that.