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
How do you print two variables without a space between them?
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
When I do something like this:
a = 1
b = 2
print(a, b)
It outputs: 1 2. I want it to output: 12. Forgive me if this is a dumb question lol
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))