Perhaps you want strutils.escape? I.e.: import strutils echo escape("\n\t")
Output is "\x0A\x09" Its output is designed to be as portable an 'input' to de-escapers/escape-interpreters as possible. So you will see things like \x0A and \x09 instead of the \n \t because the former syntax is accepted by more string parsers.