Display special characters when using print statement

Use repr:

a = "Hello\tWorld\nHello World"
print(repr(a))
# 'Hello\tWorld\nHello World'

Note you do not get \s for a space. I hope that was a typo…?

But if you really do want \s for spaces, you could do this:

print(repr(a).replace(' ',r'\s'))

Leave a Comment