Reputation: 617
Is there a way to print all characters in python, even ones which usually aren't printed?
For example
>>>print_all("skip
line")
skip\nline
Upvotes: 5
Views: 12670
Reputation: 160437
Even easier, cast it to a raw string by using "%r"
, raw strings treat backslashes as literal characters:
print("%r" % """skip
line""")
skip\nline
Additionally, use !r
in a format
call:
print("{0!r}".format("""skip
line"""))
for similar results.
Upvotes: 1
Reputation: 191728
Looks like you want repr()
>>> """skip
... line"""
'skip\nline'
>>>
>>> print(repr("""skip
... line"""))
'skip\nline'
>>> print(repr("skip line"))
'skip\tline
So, your function could be
print_all = lambda s: print(repr(s))
And for Python 2, you need from __future__ import print_function
Upvotes: 8