-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,093
  • 4
  • 25
  • 39

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,476
  • 1
  • 25
  • 38
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
  • 412
  • 2
  • 6
  • 12