Elisp: Format String

By Xah Lee. Date: . Last updated: .
format
(format STRING &rest OBJECTS)

Convert lisp objects into a string. It takes a input string, and several other arguments of lisp objects, and output a string.

  • The format control string may contain placeholders %c. The placeholders are replaced by argument to format.
  • Different placeholders means convert the argument to a string in particular way.
(format "Name: %s, age: %d" "jane" 25)
;; "Name: jane, age: 25"
;; decimal to hex. Returns 「a」
(format "%x" 10)
;; hexadecimal 「a」 to decimal. Returns 「10」.
(format "%d" #xa)
%s
string. Converted by princ. [see Elisp: Print, Output]
%d
number to decimal.
%o
number to octal notation.
%x
number to hexadecimal notation.
%X
like %x, but uses upper case.
%e
number to exponential notation.
%f
number to decimal-point notation.
%g
number to exponential notation or decimal-point notation, whichever uses fewer characters.
%c
integer (Codepoint (Character ID)) to character.
%S
any lisp object to lisp syntax (using prin1). [see Elisp: Print, Output]

Reference

Emacs Lisp String