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
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:
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:
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 deferredlength 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:
character(len=*), parameter :: version = 'v2.1.0' ! length 6Substrings
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:
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 substrings2025
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.
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 compareT
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:
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_array1. kiwi (4 letters)
2. banana (6 letters)
3. fig (3 letters)
4. clementine (10 letters)
Tip: Printing a fixed-length string with
akeeps its trailing blanks, which is perfect for aligned columns (as above) but adds unwanted spaces inside sentences. Usetrim()whenever the string is followed by more text.
Test yourself
After character(len=8) :: s and s = 'cat', what is len(s)?
- 3
- 8
- 5
- 0
Which operator concatenates strings?
+&//..
How do you get the third character of a string s?
s(3)s[3]s(3:3)s(2:3)