Emacs Lisp: Format String

By Xah Lee. Date: . Last updated: .
(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 Emacs Lisp: Print, Output]
%d
number in decimal.
%o
number converted to octal notation.
%x
number converted to hexadecimal notation.
%X
like %x, but uses upper case.
%e
number in exponential notation.
%f
number in decimal-point notation.
%g
number in exponential notation or decimal-point notation, whichever uses fewer characters.
%c
number as a single character.
%S
any object in lisp syntax (using prin1). [see Emacs Lisp: Print, Output]

(info "(elisp) Formatting Strings")

Emacs Lisp String

Lisp Basics


Lisp Basics

Basics

Lisp Data Structure

Function

Lisp Symbol

Lisp Misc

Working with Elisp