I am trying to output

Humidity: 42% 

using both functions print and message. While print has no problem with the literal percentage sign, message throws a "Format string ends in middle of format specifier". What am I missing?

(defun demo()
    (setq text (format "Humidity: %s%%" 42))
    (print text)
    (message text)
(global-set-key (kbd "<f5>") 'demo)

By running (message (format ...)), you're using the string as a format twice.

print takes an object as its parameter, while message takes a format string. Use "%s" as the format and you can specify the string as the second optional argument to message:

(setq text (format "Humidity %s%%" 42))
(print text)
(message "%s" text)
  ⇒ "Humidity 42%"

or don't run format on text before sending it to message:

(setq value 42)
(setq format "Humidity %s%%")
(print (format format value))
(message format value)
  ⇒ "Humidity 42%"
  • @NickD: The print line was missing in one of them.
    – choroba
    Jun 21 at 17:33

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.