Formatting the return string

Two Fer
Two Fer in F#

There are various ways in which you can format the return string.

Option 1: string interpolation

String interpolation was introduced in F# 5.0 and is the most idiomatic way to build up a string with one more variable parts.

$"One for {name}, one for me.";
Note

It is possible to used typed interpolations, prefixing an interpolation with its type:

$"One for %s{name}, one for me.";

This allows the compiler to check at compile time if the passed-in value has the correct type.

Option 2: string concatenation

As there are few variable parts in the returned string (just one), regular string concatentation works well too:

"One for " + name + ", one for me.";

It is slightly more verbose than string interpolation, but still completely reasonable.

Option 3: using sprintf

Before string interpolation was introduced in C# 5, sprintf was the go-to option for dynamically formatting strings.

sprintf "One for %s, one for me.", name
Note

Unlike most other languages, a sprintf call in F# is type-checked at compile time, meaning you'll get a compile time error if you're passing in an incorrect value.

String interpolation is often preferred over sprintf for its conciseness, but sprintf does have the benefit of it being a function, for example enabling partial application.

Conclusion

String interpolation is the preferred and idiomatic way to format strings, which used to be sprintf formatting. String concatentation is absolutely a viable option too, as there is only one variable part.

13th May 2025 · Found it useful?