017. Character variables are padded with spaces at the end if necessary#

topic: Character variables

Fortran character variables are padded with spaces at the end if necessary. The // operator concatenates. trim removes trailing spaces.1

char-pad.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
character (len=5) :: v = "one"

print *, "'" // v // "'"  ! 'one  '
print *, "'" // trim(v) // "'"  ! 'one'
print *, len(v), len_trim(v)  ! 5 3

end
Output2#
 'one  '
 'one'
           5           3


1

Use trim(adjustl(s)) to remove both leading and trailing spaces.

2

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags