Don't use print ..., (with a trailing comma) if you don't want spaces. Use string concatenation or formatting.
Concatenation:
print 'Value is "' + str(value) + '"'
Formatting:
print 'Value is "{}"'.format(value)
The latter is far more flexible, see the str.format() method documentation and the Formatting String Syntax section.
You'll also come across the older % formatting style:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
but this isn't as flexible as the newer str.format() method.
In Python 3.6 and newer, you'd use a formatted string (f-string):
print(f"Value is {value}")
Answer from Martijn Pieters on Stack OverflowDon't use print ..., (with a trailing comma) if you don't want spaces. Use string concatenation or formatting.
Concatenation:
print 'Value is "' + str(value) + '"'
Formatting:
print 'Value is "{}"'.format(value)
The latter is far more flexible, see the str.format() method documentation and the Formatting String Syntax section.
You'll also come across the older % formatting style:
print 'Value is "%d"' % value
print 'Value is "%d", but math.pi is %.2f' % (value, math.pi)
but this isn't as flexible as the newer str.format() method.
In Python 3.6 and newer, you'd use a formatted string (f-string):
print(f"Value is {value}")
Just an easy answer for the future which I found easy to use as a starter:
Similar to using end='' to avoid a new line, you can use sep='' to avoid the white spaces...for this question here, it would look like this:
print('Value is "', value, '"', sep = '')
May it help someone in the future.
How do i remove spaces in a print
printing - Print without space in python 3 - Stack Overflow
python - How to print without a newline or space - Stack Overflow
string - Printing in Python without a space - Stack Overflow
Im just starting out and for a assignment i need to print this line: Print (variable_a, โfiller textโ) But there canโt be a space between the two. How do i remove this space?
You can use the sep parameter to get rid of the spaces:
>>> print("a","b","c")
a b c
>>> print("a","b","c",sep="")
abc
I don't know what you mean by "Java style"; in Python you can't add strings to (say) integers that way, although if a and b are strings it'll work. You have several other options, of course:
>>> print("a = ", a, ", b = ", b, sep="")
a = 2, b = 3
>>> print("a = " + str(a) + ", b = " + str(b))
a = 2, b = 3
>>> print("a = {}, b = {}".format(a,b))
a = 2, b = 3
>>> print(f"a = {a}, b = {b}")
a = 2, b = 3
The last one requires Python 3.6 or later. For earlier versions, you can simulate the same effect (although I don't recommend this in general, it comes in handy sometimes and there's no point pretending otherwise):
>>> print("a = {a}, b = {b}".format(**locals()))
a = 2, b = 3
>>> print("b = {b}, a = {a}".format(**locals()))
b = 3, a = 2
The actual syntax of the print() function is
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
You can see it has an arg sep with default value ' '. That's why space gets inserted in between.
print("United","States") #Output: United States
print("United","States",sep="") #Output: UnitedStates
In Python 3, you can use the sep= and end= parameters of the print function:
To not add a newline to the end of the string:
print('.', end='')
To not add a space between all the function arguments you want to print:
print('a', 'b', 'c', sep='')
You can pass any string to either parameter, and you can use both parameters at the same time.
If you are having trouble with buffering, you can flush the output by adding flush=True keyword argument:
print('.', end='', flush=True)
Python 2.6 and 2.7
From Python 2.6 you can either import the print function from Python 3 using the __future__ module:
from __future__ import print_function
which allows you to use the Python 3 solution above.
However, note that the flush keyword is not available in the version of the print function imported from __future__ in Python 2; it only works in Python 3, more specifically 3.3 and later. In earlier versions you'll still need to flush manually with a call to sys.stdout.flush(). You'll also have to rewrite all other print statements in the file where you do this import.
Or you can use sys.stdout.write()
import sys
sys.stdout.write('.')
You may also need to call
sys.stdout.flush()
to ensure stdout is flushed immediately.
For Python 2 and earlier, it should be as simple as described in Re: How does one print without a CR? by Guido van Rossum (paraphrased):
Is it possible to print something, but not automatically have a carriage return appended to it?
Yes, append a comma after the last argument to print. For instance, this loop prints the numbers 0..9 on a line separated by spaces. Note the parameterless "print" that adds the final newline:
>>> for i in range(10):
... print i,
... else:
... print
...
0 1 2 3 4 5 6 7 8 9
>>>
Use print() function with sep=', ' like this::
>>> print(one, two, three, sep=', ')
1, 2, 3
To do the same thing with an iterable we can use splat operator * to unpack it:
>>> print(*range(1, 5), sep=", ")
1, 2, 3, 4
>>> print(*'abcde', sep=", ")
a, b, c, d, e
help on 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 use the Python string format:
print('{0}, {1}, {2}'.format(one, two, three))