I understand that we should use %s
to concatenate a string rather than +
in Python.
I could do any of:
hello = "hello"
world = "world"
print hello + " " + world
print "%s %s" % (hello, world)
print "{} {}".format(hello, world)
print ' '.join([hello, world])
But why should I use anything other than the +
? It's quicker to write concatenation with a simple +
. Then if you look at the formatting string, you specify the types e.g. %s
and %d
and such. I understand it could be better to be explicit about the type.
But then I read that using +
for concatenation should be avoided even though it's easier to type. Is there a clear reason that strings should be concatenated in one of those other ways?