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?
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?
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)
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
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="");
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="")
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.
To do this, just comma after print for example:
'for item in "California":
if item == "i":
print "x",
else:
print item,'