how can I output text to the console without new line at the end? for example:
print 'temp1' print 'temp2'
output:
temp1 temp2
And I need:
temp1temp2
The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.
In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")
Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.
In python2. x you can add a comma (,) at the end of the print statement that will remove newline from print Python.
Add a comma after the last argument:
print 'temp1', print 'temp2'
Alternatively, Call sys.stdout.write
:
import sys sys.stdout.write("Some output")
In Python > 2.6 and Python 3:
from __future__ import print_function print('temp1', end='') print('temp2', end='')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With