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?
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'
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')
Look here
How to print a string literally in Python
z = 'hellon hello'
print(repr(z))
>>> hellon hello
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?
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'
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')
Look here
How to print a string literally in Python
z = 'hellon hello'
print(repr(z))
>>> hellon hello