How to print a string as it is written?

Question:

Let’s say that I have a string like this: ‘Hello n this world is nice’. When calling the print function print(‘Hello n this world is nice’) the result would be such that a new line is introduced after ‘Hello’.

If I wanted to print exactly

‘Hello n this world is nice’

so such that n appears in the output of the print function as two characters), how could I do it?

Asked By: Ile

||

Answers:

One way could be using [Python.Docs]: Built-in Functions – repr(object):

txt = "Hello n this world is nice"
>>> print(txt)
Hello
 this world is nice
>>> print(repr(txt))
'Hello n this world is nice'
Answered By: CristiFati

Escape the to print is as literal:

print('Hello \n this world is nice')

Or add an r do interpret all in the string as literal (raw):

print(r'Hello n this world is nice')
Answered By: b3nj4m1n

Look here

How to print a string literally in Python

z = 'hellon hello' 
print(repr(z))
>>> hellon hello
Answered By: marsnebulasoup
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.