Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output without new line

Tags:

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 
like image 371
Max Frai Avatar asked Apr 12 '10 16:04

Max Frai


People also ask

How do I print without starting a new line?

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.

How can you force terminal output without a newline?

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.")

How do I display output on the same line?

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.

How do I get rid of the default new line in Python?

In python2. x you can add a comma (,) at the end of the print statement that will remove newline from print Python.


2 Answers

Add a comma after the last argument:

print 'temp1', print 'temp2' 

Alternatively, Call sys.stdout.write:

import sys sys.stdout.write("Some output") 
like image 138
SLaks Avatar answered Sep 24 '22 03:09

SLaks


In Python > 2.6 and Python 3:

from __future__ import print_function  print('temp1', end='') print('temp2', end='') 
like image 32
Olivier Verdier Avatar answered Sep 20 '22 03:09

Olivier Verdier