A deferred-length allocatable character(len=:) auto-resizes on assignment, perfect for building strings.

Program

Play the program to concatenate two parts with a space.

string_concat.f90
Replay: real traced execution (multi-file project)
program string_concat
    implicit none
    character(len=:), allocatable :: first, last, full
    first = "Modern"
    last = "Fortran"
    full = first // " " // last
    print '(A)', full
end program string_concat
  1. first ← Modern

    3character(len=:), allocatable :: first, last, full4first = "Modern"5last = "Fortran"
    values this stepModernfirst
  2. last ← Fortran

    4first = "Modern"5last = "Fortran"6full = first // " " // last
    values this stepFortranlast
  3. full ← Modern Fortran

    5last = "Fortran"6full = first // " " // last7print '(A)', full
    values this stepModern FortranfullModernfirstFortranlast
  4. print '(A)', full

    6    full = first // " " // last7    print '(A)', full8end program string_concat
    outputModern Fortran
    values this stepModern Fortranfull

Follow the Join

  1. first holds Modern.
  2. last holds Fortran.
  3. first // " " // last joins the first word, one space, and the last word.
  4. full becomes Modern Fortran.
  5. The program prints Modern Fortran. | part | value | | --- | --- | | first | Modern | | spacer | one blank | | last | Fortran | | full | Modern Fortran |
deferred length `character(len=:), allocatable` sizes the string on assign.
// concat `a // b` joins two strings end-to-end.
auto-allocate Assignment to an unallocated deferred-length string allocates the right length.

Exercise: string_concat.f90

Reproduce Modern Fortran, then trace how the two words and the one blank become the final string.