Search code examples
pythonline-breaks

Python linebreak '\n' is not working when I include something at end


The Python linebreak command \n doesn't work for me on Python 2.7 when I include something in the statement, like an int or a numpy array. Is there a way to do this? Here are some examples:

print("These \n linebreaks don't work:\n", 1)
"These \n linebreaks don't work:\n", 1

print("These \n work fine\n")
These 
 work fine

Solution

  • If you want to use print like a function, import the one from Python3.

    >>> from __future__ import print_function
    >>> print("These \n linebreaks don't work:\n", 1)
    These 
     linebreaks don't work:
     1
    

    Now they actually do and you won't have to change anything.