7

If I need to print a single newline, what is the most Pythonic way to write the print() statement? I am teaching someone Python and I want to do it right.

The two ways that I found most likely were:

1) print("")

  • Pros: Shorter and more simple
  • Cons:
    • Might be less readable due to no mention of \n
    • Relies on end being a newline, which may not be inherently obvious

2) print("\n", end = "")

  • Pros: It is obvious what the statement is meant to do
  • Cons: Longer. Beginner might not know what the 'end' argument means.
MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 7
    `print()` is shorter – depperm Aug 08 '17 at 18:33
  • As much as I like this question, shouldn't asking for the most "pythonic" way to do something be asked on codereview? This seems opinionated – Ryan Aug 08 '17 at 18:41
  • @Ryan Usually, yes. In this case, there's simply not enough to review. – Mast Aug 08 '17 at 18:42
  • @depperm" I often use `print('')` because it will does the same thing in both Python 2 & 3. `print()` will output `()\n` in Python 2, not just `\n` as in Python 3, so using `print('')` is more portable. – martineau Aug 08 '17 at 19:01
  • 6
    @martineau I don't think that's a good reason to write weird-looking Python 3 code. If you want to write portable code, you would use `from __future__ import print_function`. – wim Aug 08 '17 at 19:26
  • 2
    @martineau: always use `from __future__ import ...` statements when writing code that has to work on 2 & 3. `print_function` is your friend, but not the only one you should use. – Martijn Pieters Aug 08 '17 at 21:46
  • @wim: For new code, sure, but using `from __future__ import` isn't always an option when working on existing code (or when answering questions here where it's unclear what version is being used). In those situations using `print('')` is worth the slightly weird-looking construct IMO. – martineau Aug 09 '17 at 00:03
  • @Martijn: What is said in my response to @ wim... – martineau Aug 09 '17 at 00:04
  • 2
    @martineau: **just refactor that code**. It's not a big change to go from `print ...` to `print(...)`. We did this at Facebook; require that all Python-2 compatible code *must* have those `__future__` imports. We are well on track with replacing all Python 2 code with Python 3 now; all new code is always Python 3. See https://www.youtube.com/watch?v=nRtp9NgtXiA – Martijn Pieters Aug 09 '17 at 06:51
  • 1
    @martineau: Python 2.x has been sunsetted. Everyone's supposed to be on 3.x now. Answers should address 3.x. And it's trivial to add `from __future__ import print_function` to migrate old 2.x code, people have been doing it [for the last 8 years](https://stackoverflow.com/questions/7075082/what-is-future-in-python-used-for-and-how-when-to-use-it-and-how-it-works) – smci Sep 07 '19 at 23:46
  • 1
    @smci (and everyone else): I wrote that comment about using `print('')` over 2 years ago when many more folks were still using Python 2 — and I totally agree it's not (that) relevant nowadays... – martineau Sep 08 '19 at 00:13
  • @martineau: sure but it's high time to delete the comment. People (like me) are still finding this question today using keyword search; it might get used as a dupe target. – smci Sep 08 '19 at 02:29

3 Answers3

18

Neither. The usual way would be:

print()
wim
  • 338,267
  • 99
  • 616
  • 750
4

Most simple and shorter method is :

print()
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
3

Note in Python 2.x printing a newline is a simple as:

print
Ajax1234
  • 69,937
  • 8
  • 61
  • 102