# Equivalent of str function (from python) in Fortran

**URL:** <https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683>\
**Category:** Help\
**Created:** [January 29, 2022, 3:39am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683 "2022-01-29T03:39:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [January 29, 2022, 3:39am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/1 "2022-01-29T03:39:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [January 29, 2022, 4:15am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/2 "2022-01-29T04:15:47Z")

</div>

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

```auto
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'

```

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [January 29, 2022, 4:18am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/3 "2022-01-29T04:18:03Z")

</div>

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

- [fortran-utils/utils.f90 at b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88 · certik/fortran-utils · GitHub](https://github.com/certik/fortran-utils/blob/b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88/src/utils.f90#L12)
- [fortran-utils/utils.f90 at b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88 · certik/fortran-utils · GitHub](https://github.com/certik/fortran-utils/blob/b43bd24cd421509a5bc6d3b9c3eeae8ce856ed88/src/utils.f90#L298)

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

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [January 29, 2022, 4:40am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/4 "2022-01-29T04:40:54Z")

</div>

There is [`to_string`](https://stdlib.fortran-lang.org/page/specs/stdlib_strings.html#to_string) in the standard library. It works with integer, real, complex, and logical inputs.

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [January 29, 2022, 7:30am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/5 "2022-01-29T07:30:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [January 29, 2022, 8:48pm UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/6 "2022-01-29T20:48:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 31, 2022, 1:40am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/7 "2022-01-31T01:40:38Z")

</div>

You might be interested in the `str` function in the fpm package M\_msg as well. Go to

[M\_msg](https://urbanjost.github.io/M_msg/man3.html)

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.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [January 31, 2022, 6:33pm UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/8 "2022-01-31T18:33:56Z")

</div>

@urbanjost’s `str` function is used as:

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

```

While mine would be used as:

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

```

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 31, 2022, 11:56pm UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/9 "2022-01-31T23:56:29Z")

</div>

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

```fortran
'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

```fortran
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'

```

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [February 1, 2022, 3:15am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/10 "2022-02-01T03:15:21Z")

</div>

thanks.  
Just for the information, [fString](https://github.com/dpettas/fString) also exists, and is an object-oriented library providing string objects.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 1, 2022, 5:04am UTC](https://fortran-lang.discourse.group/t/equivalent-of-str-function-from-python-in-fortran/2683/11 "2022-02-01T05:04:06Z")

</div>

> [@urbanjost](#):
>
> I do not believe your function defaults to padding with a space

Good catch, I forgot about that.
