0

What is the difference between print("\n") and print("\5")?

I tried below in a python shell.

Why does print("\5") output a new line:

>>> print("\n")


>>> print("\5")

>>> 

But when I tried:

print("\4")

print("\6")

It's printing some binary data

vishal yadav
  • 93
  • 1
  • 7
  • Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Alex Yu Feb 07 '19 at 13:46
  • My question is not to know about the newline print in python. It's related to what is difference between i) print("\n") ii) print("\5") iii) print("\4") Only print("\5") gives a newline but apart from 5 whatever we use inside a print with "\" like "\4", "\6" will give binary output. I hope this gives the reason why its not a duplicate one. @AlexYu – vishal yadav Feb 08 '19 at 06:39
  • Short answer (in my opinion): because `print` always outputs newline not matter what symbols you are `print`ing. But I can agree that your question is not full duplicate - it was kind of stretch from me – Alex Yu Feb 08 '19 at 09:55

1 Answers1

6

Whenever you use print in python, it puts a newline at the end. The thing you should pay attention to is how many newlines are in the output.

"\5" is just a character (it's the control characters ENQ in ASCII; while it is technically non-printable, my terminal renders it as ♣); printing it outputs whatever your terminal decides to use to render it followed by a newline. print("") will output a newline. print("\n") by contrast will output two newlines.

If your terminal can't/won't render \5 (it is a non-printable character after all), print("\5") will be the same as print("").

Zags
  • 37,389
  • 14
  • 105
  • 140