-1

Is there a way to litterally print an escape sequence such as \n? For example:

print "Hello\nGoodbye"

The output of this would be:

Hello
Goodbye

Is there a way to get it to literally print out this?

Hello\nGoodbye
Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42

3 Answers3

4

You can use raw strings (see here). Just add an r before the string:

>>> print r"hello\nworld"
hello\nworld
ebarr
  • 7,704
  • 1
  • 29
  • 40
3

You can place the string in repr:

>>> mystr = "Hello\nGoodbye"
>>> print mystr
Hello
Goodbye
>>> print repr(mystr)
'Hello\nGoodbye'
>>> # Remove apostrophes
>>> print repr(mystr)[1:-1]
Hello\nGoodbye
>>>
1

Simpy write:

r'Hello\nGoodbye'

r is for raw string.

Konrad Wąsowicz
  • 422
  • 2
  • 6
  • 12