Interpolated strings are a feature in F# that allows us to embed expressions inside string literals. The symbol denotes them before the opening quote of the string. When the string is evaluated, expressions inside the string are replaced with their values, making it easier to format strings and embed variables.
We need the $
symbol before the string to indicate that it is an interpolated string. We can use expressions in an interpolated string by enclosing it with braces {}
.
Here’s an example of an interpolated string in F#:
open System let name = "Educative" let age = 5 printfn $" {name}! You're {age} years old"
This code can be explained as follows:
Line 1: Import the System
namespace from the .NET System
library.
Line 3: Define name
, which contains the string Educative
.
Line 4: Define age
, which contains the integer 5
.
Line 6: Print both variables using string interpolation.
We can specifically format our interpolated string. It has the following format:
{<interpolatedString>:<format>}
An example of this is given in the code below:
open System let name = "Educative" let age = 5.5 printfn $" {name}! You're {age:F2} years old"
In the code above, even though the age value is 5.5
, 5.50
will be the output as we’ve applied the F2
format (a floating point number with two decimal places).
We can specify the field width in our interpolated string. This can be done by following this format:
{<interpolatedString>,<alignment>}
If we want to specify both the width and the format, it can be done as follows:
{<interpolatedString>,<alignment>:<format>}
A coding example of this is below:
open System let name = "Educative" let age = 5.5 printfn $"Name: {name,-12} | Age: {age,8:F1}"
In the code above, we’ve done the following alignments:
{name,-12}
: Specifies the name
variable to be left-aligned within a field width of 12
characters. The negative sign -
indicates left alignment.
{age,8:F1}
: Specifies the age
variable to be right-aligned within a field width of 8
characters and formatted as a floating-point number with one decimal place.
To use escape sequences in interpolated strings, we can simply include the escape sequences directly within the string literals. Here's an example:
open System let name = "Educative" let age = 5 printfn $"Hello, {name}!\nYou're {age} years old."
\n
is the escape sequence for a newline character. It will cause the text to be split into two lines when printed.
In F#, we can use the ternary conditional operator ? :
within an interpolation expression to conditionally choose between two values based on a condition. Here’s how we can do it:
open System let name = "Educative" let age = 20 printfn $"""Hello, {name}! You're {(if age >= 18 then "an adult" else "a minor")}."""
In this example:
The ternary conditional operator ? :
is used within the interpolation expression if age >= 18 then "an adult" else "a minor"
.
If the condition age >= 18
is true, the string "an adult"
will be chosen. Otherwise, the string "a minor"
will be chosen.
Interpolated strings in F# are a powerful feature that enhances string formatting and manipulation. Here, we explored various aspects of interpolated strings, from basic usage to advanced formatting techniques.
Free Resources