2
(format "Hard-coded value is 100\% and the variable is %s" "50\\%")

Returns

Debugger entered--Lisp error: (error "Invalid format operation %a")
  format("Hard-coded value is 100% and the variable is %s" "50%")
  eval((format "Hard-coded value is 100% and the variable is %s" "50%") nil)
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

(Note how in the debugger output, the command has the "%" escaped)

How in heaven can I use a string with a percentage in it?

  • 1
    I think it's %%. – politza Jul 19 '17 at 22:16
  • Elisp manual, node Formatting Strings. You can get to that just by using C-h i, choosing the Elisp manual, and i format RET. – Drew Jul 20 '17 at 21:53
6

It is indeed %%. If you type C-h f format <RET> you'll pull up the documentation for the format function, which states:

Use %% to put a single % into the output.

Thus:

(format "Hard-coded value is 100%% and the variable is %s" "50%")

(the % signs never need to be escaped with backslashes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.