Is there a way to print everything without the line break?

5

I have this code:

print "t"
print "e"
print "s"
print "t"
print "e"

It has the following output:

t
e
s
t
e

What I find annoying "\n" .

Is there a way to print everything without the line break?

    
asked by anonymous 08.08.2016 / 17:51

6 answers

2
print 't' + 'e' + 's' + 't' + 'e'

Print by default always makes a line break at the end, if you want, you can write directly to sys.stdout that will not get line break

import sys
sys.stdout.write('teste')

Now the print function itself in the new python > 3.0 gives you the choice of the separator and the end of the line: see function documentation and default values:

print(...)
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
  

Prints the values to a stream, or to sys.stdout by default. Optional   keyword arguments: file: a file-like object (stream); defaults to the   current sys.stdout. sep: string inserted between values, default a   space. end: string appended after the last value, default to newline.   flush: whether to forcibly flush the stream.

You can do it in python3:

print('teste', end='', flush=True)
    
08.08.2016 / 18:04
14

If you are using python 3.x:

print('t', end="")
print('e', end="")
print('s', end="")
print('t', end="")
print('e')

If not (if it is python2.x) the easiest way to not import unnecessary things would be to include all the characters in the same string:

print 'teste' # python2.x

OR

print('teste') # python3.x

I imagine this might be coming from a cycle running characters and is doing print right inside the cycle, so instead of doing print right inside the cycle, why not do it like this:

str = 'teste'
final = ''
for i in str:
    final += i
print(final) # python 3.x
#print final #python 2

RELATED

    
08.08.2016 / 17:56
6

In fact, the context of the question does not seem like much to be used in day-to-day programming.

It does not make much sense to want to print letter by letter with print , if you can do so directly:

print "teste"

There are cases where you may want to have this string separated into each letter. In this case you can do

test = "teste"

list(test); # ['t', 'e', 's', 't', 'e']

In python 3, if you needed to print all the values of a list , without separating line by line, you could do this:

  print (*['t', 'e', 's', 't', 'e'], sep="")

  #ou

  teste = list("teste");

  print(*test, sep="");

  #ou

  test = ['t', 'e', 's', 't', 'e']

  print(*test, sep="");
    
08.08.2016 / 18:02
3

If you are using python 2.7 you can do the following:

print "t",
print "e",
print "s",
print "t",
print "e"

If you also do not want the space between characters:

import sys
print "t",
sys.stdout.write("")
print "e",
sys.stdout.write("")
print "s",
sys.stdout.write("")
print "t",
sys.stdout.write("")
print "e"

If you are using python 3.5:

print("t", end="")
print("e", end="")
print("s", end="")
print("t", end="")
print("e", end="")
    
09.08.2016 / 13:52
2

My friend, I thought of something much simpler. I created a function to receive a parameter, and use it with print () and end="" :

def printf (text):
    print(text, end="")

And right, every time I want to write something I use the function created printf () For example:

printf("Olá como")
printf("você está")
printf("hoje ?")

returns:

Olá como você está hoje ?

Hugs.

    
01.07.2017 / 22:19
1

To do this, just comma after print for example:

'for item in "California":
  if item == "i":
    print "x",
  else:
    print item,'
    
11.10.2018 / 00:52