Python printing without space

I found this problem in several different places, but mine is a little different, so I cannot use and apply the answers. I do the exercises in the Fibonacci series, and because for school I do not want to copy my code, but here is something very similar.

one=1
two=2
three=3
print(one, two, three)

When it is printed, "1 2 3" is displayed. I do not want this, I would like it to display it as "1,2,3" or "1, 2, 3", I can do this using this change

one=1
two=2
three=3
print(one, end=", ")
print(two, end=", ")
print(three, end=", ")

My real question is: is there a way to condense these three lines of code into one line, because if I put them all together, I get an error.

Thanks.

+4
source share
5

print() sep=', ' :

>>> print(one, two, three, sep=', ')
1, 2, 3

, splat *, :

>>> print(*range(1, 5), sep=", ")
1, 2, 3, 4
>>> print(*'abcde', sep=", ")
a, b, c, d, e

help 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.
+5

Python format:

print('{0}, {1}, {2}'.format(one, two, three))
+3

You can do this with or without a comma:

1) No spaces

one=1
two=2
three=3
print(one, two, three, sep="")

2) Comma with space

one=1
two=2
three=3
print(one, two, three, sep=", ")

3) Comma without space

one=1
two=2
three=3
print(one, two, three, sep=",")
+3
source

Another way:

one=1
two=2
three=3
print(', '.join(str(t) for t in (one,two,three)))
# 1, 2, 3
+1
source

you can also try:

print("%d,%d,%d"%(one, two, three))
0
source

All Articles