The syntax for string literals is extended to support string interpolation.
An interpolated string literal starts with f
, immediately before
the first double-quote character.
Within an interpolated string literal, an arbitrary expression, when
enclosed in { ... }
, is expanded at run time into the result of calling
'Image
on the result of evaluating the expression enclosed by the brace
characters, unless it is already a string or a single character.
Here is an example of this feature where the expressions Name
and X + Y
will be evaluated and included in the string.
procedure Test_Interpolation is X : Integer := 12; Y : Integer := 15; Name : String := "Leo"; begin Put_Line (f"The name is {Name} and the sum is {X + Y}."); end Test_Interpolation;
This will print:
The name is Leo and the sum is 27.
In addition, an escape character (\
) is provided for inserting certain
standard control characters (such as \t
for tabulation or \n
for
newline) or to escape characters with special significance to the
interpolated string syntax, namely "
, {
, }
,and \
itself.
escaped_character | meaning |
\a | ALERT |
\b | BACKSPACE |
\f | FORM FEED |
\n | LINE FEED |
\r | CARRIAGE RETURN |
\t | CHARACTER TABULATION |
\v | LINE TABULATION |
\0 | NUL |
\\ | \ |
\" | " |
\{ | { |
\} | } |
Note that, unlike normal string literals, doubled double-quote characters have no
special significance. So to include a double-quote or a brace character
in an interpolated string, they must be preceded by a \
.
Multiple interpolated strings are concatenated.
For example:
Put_Line (f"X = {X} and Y = {Y} and X+Y = {X+Y};\n" & f" a double quote is \" and" & f" an open brace is \{");
This will print:
X = 12 and Y = 15 and X+Y = 27 a double quote is " and an open brace is {