I'm new to Python, and I'm wondering how to print multiple values without having the extra space added in between. I want the output ab
rather than a b
without having to call print
twice:
print("a", end="")
print("b")
Also, I have the following code:
a = 42
b = 84
and I want to print their values as a = 42, b = 84
, yet if I do
print("a = ", a, ", ", b = ", b)
extra spaces are added (it outputs a = 42 , b = 84
)
Whereas the Java style,
print("a = " + a + ", b = " + b)
raises a TypeError
.
On this page the answer is to print your normal text and at the end to use
sep=""
.So the command
will give
assuming that
hole==1
andpar==4
.You can use the
sep
parameter to get rid of the spaces: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
andb
are strings it'll work. You have several other options, of course: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):
You can also use
to print a and b not seperated by spaces in form of a formatted string. This is similar to the one in c.