Display a term with the character escape sequences intact

Sometimes it desirable to print (write) a term with the character escape sequences intact (syntactically) as opposed to them being performed (semantically), e.g. print \n instead of performing a new line.

write_canonical/1 is the sword of choice in such situations.

?- write("string\n\n").
string

true.

?- write_canonical("string\n\n").
"string\n\n"
true.

Since format/2,3 is often used for displaying output, write_canonical/1 is an available option using the special sequence ~k.

?- format('~w',["string\n\n"]).
string

true.

?- format('~k',["string\n\n"]).
"string\n\n"
true.