Skip to content

format

Printf-style string formatting.

Syntax

tcl
format formatString ?arg ...?

Parameters

  • formatString: A format string containing literal text and conversion specifiers
  • arg: Values to substitute into the format string

Format Specifiers

SpecifierDescription
%d, %iSigned decimal integer
%uUnsigned decimal integer
%oOctal integer
%x, %XHexadecimal integer (lowercase/uppercase)
%bBinary integer
%cCharacter (from integer code)
%sString
%fFloating-point (fixed notation)
%e, %EFloating-point (exponential notation)
%g, %GFloating-point (shorter of %f or %e)
%a, %AFloating-point (hexadecimal)
%pPointer (platform-specific)
%%Literal percent sign

Flags

FlagDescription
-Left-justify within field width
0Pad with zeros instead of spaces
+Always show sign for numeric values
#Alternate form (prefix for octal/hex)
Space before positive numbers

Width and Precision

  • width: Minimum field width (e.g., %10s)
  • .precision: For floats, decimal places; for strings, max length (e.g., %.2f)
  • positional: Use %n$ to reference argument by position (e.g., %2$s)

Examples

Basic formatting

Output

Number formatting

Output

Floating-point formatting

Output

Width and alignment

Output

Positional arguments

Output

Character conversion

Output

See Also

  • scan - Parse strings with format
  • string - String operations
  • subst - Variable and command substitution