📚Cheatsheets

Cheatsheet collection for go, rust, python, shell and javascript.

How to format string in OCaml?

In OCaml, you can use the Printf module to format strings. Here is an example:

let name = "John"
let age = 30
let height = 180.5

let formatted_string = Printf.sprintf "Name: %s, Age: %d, Height: %.1f" name age height

(* The value of formatted_string is now: "Name: John, Age: 30, Height: 180.5" *)

In this example, we use the Printf.sprintf function to create a formatted string. The first argument to this function is the format string, which specifies how the remaining arguments should be formatted.

In the format string, %s is used to indicate that the next argument should be formatted as a string, %d is used to indicate that the next argument should be formatted as an integer, and %.1f is used to indicate that the next argument should be formatted as a floating-point number with one decimal place.

You can use a variety of format specifiers in the format string to control how the remaining arguments are formatted. For a complete list of format specifiers and examples, you can refer to the Printf module documentation.