YAKOVM
YAKOVM

Reputation: 10153

Escape new line in triple quoted string - python

I have following string

string = """
Hello/nWorld
123
HelloWorld
"""

But when I print it I get

Hello
World
123
HelloWorld

But I want to get

Hello/nWorld
123
HelloWorld

Upvotes: 0

Views: 1293

Answers (2)

Gsk
Gsk

Reputation: 2945

Add a \ to escape the other \ char:

string =  """
Hello\\nWorld
123
HelloWorld
"""

or have a "raw" string:

string =  r"""
Hello\nWorld
123
HelloWorld
"""

Upvotes: 2

user9163363
user9163363

Reputation:

Your code is doing exactly what you want it to do, at least on Linux:

>>> string = """
... Hello/nWorld
... 123
... HelloWorld
... """
>>> print(string)

Hello/nWorld
123
HelloWorld

Are you perhaps on a system that uses /n as newline?

Upvotes: 1

Related Questions