Simplified Common Lisp reference
format
Symbol class: Input and output
Syntax:
Symbol type: function
formatdestinationcontrol-stringargs(zero or more) => string or NIL
Argument description:
destination T, NIL, stream or string with fill-pointer
control-string a string with formating directives
args format arguments for control-string

FORMAT function does a complex text formatting. Formatting rules are driven by control-string and arguments in arg. When destination is stream or string with fill-pointer, the resulting string is written to it. T as a destination means "write to terminal". NIL as destination means "return the formatted string back as string". See also WRITE-STRING, TERPRI, PRINC, PRIN1 and PRINT.

Control string is composed of normal text and embedded directives. Directives begin with tilde (~) character. Most common are: ~a - output with aesthetics, ~s - standard output, ~% newline, tilde parenthesis - flow control, tilde tilde - escape sequence for tilde. See full documentation or examples for more.

(format nil "Items in list:~%~{~a, ~}" '(1 2 3 4)) => "Items in list:
1, 2, 3, 4, "
(format nil "~{~a~^, ~}" '(1 2 3 4)) => "1, 2, 3, 4"
(format nil "~f" 3.141592) => "3.141592"
(format nil "~2,3f" 3.141592) => "3.142"
(format nil "~7,3f" 3.141592) => "  3.142"
(format nil "~a ~s" "xyz" "xyz") => "xyz \"xyz\""
Function indexFull documentation for format (HyperSpec)