Node:Formatted output, Previous:Iterators, Up:Streams



Formatted output

To format data as a string, do:

sprintf format_string arg_1 ... arg_n
The args are formatted according to formatting directives in the string format_string.

To format data as a string, and send the result to the output text stream out_stream do:

out_stream printf format_string arg_1 ... arg_n
If out_stream is omitted, it defaults to the standard output file. Except for some pathological cases (and efficiency), this is the same as:
out_stream put (sprintf format_string arg_1 ... arg_n)@

An example:

:x=12
sprintf "[x=%05d]" 12      # Yields the string "[x=00012]"
The text "%05d" is a format directive which indicates that the argument value (in this case 12) is to be written using 5 decimal digits, filled with zeros on the left. The rest of the format string ("[x=" and "]") is emitted verbatim into the output.

A format control string is written in a form (syntax) similar to the printf control strings in the C language. However, the semantics is closer to the format control strings in Common Lisp: formatting is is quite powerful, and can take arguments that are typed.