typestar

Strings and Printf in OCaml

Concatenate with ^, format with %s and %.1f, sprintf returns.

(* ^ concatenates; Printf formats with compile-checked specifiers. *)
let pool = "ledge pool"
let temp_c = 14.5

let () =
  print_endline ("Sampling the " ^ pool);
  Printf.printf "%s at %.1f C\n" pool temp_c;
  (* sprintf returns the string instead of printing. *)
  let label = Printf.sprintf "%s-%d" pool 2026 in
  print_endline ("Label: " ^ label);
  Printf.printf "Uppercase: %s\n" (String.uppercase_ascii pool)

How it works

  1. Printf.printf checks its format against the arguments at compile time.
  2. Printf.sprintf builds the string instead of printing it.
  3. String.uppercase_ascii names its encoding honestly.

Keywords and builtins used here

The run, in numbers

Lines
11
Characters to type
404
Tokens
74
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 68 seconds.

Type this snippet

Step 3 of 3 in Values, step 3 of 29 in Language basics.

← Previous Next →