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

Asked

Viewed 52,205 times

11

I have this code:

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

He’s got a way out:

t
e
s
t
e

What I find very boring these "\n".

There is a way to print everything without line break?

7 answers

21

If you are using python 3.x:

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

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

print 'teste' # python2.x

OR

print('teste') # python3.x

I imagine that it might be coming out of a cycle going through characters and it’s 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

8

In fact, the context of the question seems not to be something to be widely used in day-to-day programming.

It doesn’t make much sense for you to want to print letter by letter with the print, if you can do so directly:

print "teste"

There are cases yes, where you might 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 line separation, you could do so:

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

  #ou

  teste = list("teste");

  print(*test, sep="");

  #ou

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

  print(*test, sep="");

4

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 don’t want the character spacing:

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

4

Buddy, 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 so, 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.

  • 1

    Thank you. I will be posting a question about Python graphics in a little while. I can leave the link here?

  • 1

    https://answall.com/questions/217370/gr%C3%a1fico-em-python-n%C3%a3o-displays-all-desired values

1

n = int(raw_input())
i=1
while(i<=n):
    print(i ,end="" )        
    i+=1

1

For this just by a comma after the print for example:

`for item in "California":
  if item == "i":
    print "x",
  else:
    print item,`

1


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

The default print always does a line break at the end, if you want, you can write straight into sys.stdout which will not have line break

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

Now the print function itself in the newer python >3.0 gives to choose the separator and the end in 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 a newline. flush: whether to forcibly flush the stream.

you can do in python3:

print('teste', end='', flush=True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.