TExcelTAD
Home › Fortran › Fortran Strings

Fortran Strings

Text in Fortran is stored in variables of type character. The most important thing to understand is that a classic Fortran string has a fixed length: it always contains exactly that many characters, padded with blanks on the right if the value is shorter.

Fixed-length strings

fortran · try it yourself
program fixed_length
  implicit none
  character(len=10) :: city
  character(len=3)  :: code

  city = 'Lyon'            ! shorter: padded with 6 blanks
  code = 'Barcelona'       ! longer: truncated to 3 characters

  print '(3a)', '[', city, ']'
  print '(3a)', '[', code, ']'
  print '(a, i0)', 'len(city)      = ', len(city)
  print '(a, i0)', 'len_trim(city) = ', len_trim(city)
  print '(3a)', '[', trim(city), ']'
end program fixed_length
[Lyon      ]
[Bar]
len(city)      = 10
len_trim(city) = 4
[Lyon]
Function Returns
len(s) the declared length, including trailing blanks
len_trim(s) the length without trailing blanks
trim(s) s with trailing blanks removed

Strings can be written with single or double quotes. To include the quote character itself, double it or use the other kind: 'It''s' and "It's" are the same.

Concatenation with //

The // operator joins strings. Because fixed-length variables carry their padding, you'll usually trim them first:

fortran · try it yourself
program concatenate
  implicit none
  character(len=12) :: first, last
  character(len=30) :: full

  first = 'Grace'
  last = 'Hopper'
  full = first // last
  print '(3a)', '[', full, ']'

  full = trim(first) // ' ' // trim(last)
  print '(3a)', '[', trim(full), ']'
  print '(a)', 'Hello, ' // trim(first) // '!'
end program concatenate
[Grace       Hopper            ]
[Grace Hopper]
Hello, Grace!

Deferred-length strings

A deferred-length allocatable string, character(len=:), allocatable, takes the exact length of whatever you assign to it. It is the most convenient string type for building text:

fortran · try it yourself
program deferred
  implicit none
  character(len=:), allocatable :: msg
  integer :: i

  msg = 'Count:'
  print '(a, i0)', 'length now ', len(msg)
  do i = 1, 4
    msg = msg // ' ' // achar(iachar('0') + i)   ! append a digit
  end do
  print '(3a)', '[', msg, ']'
  print '(a, i0)', 'length now ', len(msg)
end program deferred
length now 6
[Count: 1 2 3 4]
length now 14

iachar(c) returns the ASCII code of a character and achar(n) the character with code n.

Assumed-length constants

For constants, len=* lets the compiler count the characters:

fortran
character(len=*), parameter :: version = 'v2.1.0'     ! length 6

Substrings

A substring is selected with s(start:end); positions count from 1 and the end is included. Either bound may be omitted. A substring can also be assigned to:

fortran · try it yourself
program substrings
  implicit none
  character(len=*), parameter :: date = '2025-07-14'
  character(len=11) :: word

  print '(a)', date(1:4)          ! year
  print '(a)', date(6:7)          ! month
  print '(a)', date(9:)           ! day
  print '(a)', date(:1)           ! first character

  word = 'Fortran 77'
  word(9:10) = '23'               ! replace part of the string
  print '(a)', word
  print '(a)', word(len_trim(word):len_trim(word))   ! last non-blank char
end program substrings
2025
07
14
2
Fortran 23
3

Note that a single character is s(i:i), not s(i): s(i) would mean element i of an array of strings.

Comparing strings

==, /=, <, > compare strings character by character. If the lengths differ, the shorter string is padded with blanks first, so 'abc' == 'abc ' is true.

fortran · try it yourself
program compare
  implicit none
  print '(l1)', 'abc' == 'abc   '
  print '(l1)', 'apple' < 'banana'
  print '(l1)', 'Zebra' < 'apple'        ! upper case comes before lower case in ASCII
  print '(l1)', lle('Zebra', 'apple')    ! lle/llt/lge/lgt: always ASCII order
end program compare
T
T
T
T

Arrays of strings

All elements of a character array have the same length. In constructors, give the type and length explicitly so that elements of different lengths are allowed:

fortran · try it yourself
program string_array
  implicit none
  character(len=10) :: fruits(4)
  integer :: i

  fruits = [character(len=10) :: 'kiwi', 'banana', 'fig', 'clementine']
  do i = 1, size(fruits)
    print '(i0, a, a, a, i0, a)', i, '. ', fruits(i), ' (', len_trim(fruits(i)), ' letters)'
  end do
end program string_array
1. kiwi       (4 letters)
2. banana     (6 letters)
3. fig        (3 letters)
4. clementine (10 letters)

Tip: Printing a fixed-length string with a keeps its trailing blanks, which is perfect for aligned columns (as above) but adds unwanted spaces inside sentences. Use trim() whenever the string is followed by more text.

Test yourself

After character(len=8) :: s and s = 'cat', what is len(s)?

  1. 3
  2. 8
  3. 5
  4. 0

Which operator concatenates strings?

  1. +
  2. &
  3. //
  4. ..

How do you get the third character of a string s?

  1. s(3)
  2. s[3]
  3. s(3:3)
  4. s(2:3)