0

So I'm trying to write this exact string but I don't \n to make a new line I want to actually print \n on the screen. Any thoughts on how to go about this? (using python

Languages:\npython\nc\njava

  • 1
    Either escape the backslash or use a raw string. – Barmar Jan 16 '20 at 01:16
  • 2
    Use a raw string like `r'foo\nbar'`, or use the ``\\`` escape sequence like `'foo\\nbar'`. – kaya3 Jan 16 '20 at 01:16
  • I'm not sure what exactly you are trying to do but in a string in python you would just use this for example: ```test = '\\n'``` So basically use an extra backslash to escape any escape characters, which is what a backslash for example is – DJSchaffner Jan 16 '20 at 01:17
  • Aye thanks a lot guys !! I got it to work!! – Kyle Johnson Jan 16 '20 at 01:19

2 Answers2

0

adding a backslash will interpret the succeeding backslash character literally. print("\\n").

Bugbeeb
  • 2,021
  • 1
  • 9
  • 26
0

Either escape the backslash by preceding it with another backslash:

'Languages:\\npython\\nc\\njava'

Or use a raw string by preceding the literal with an r:

r'Languages:\npython\nc\njava'
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328