Concatenating string and integer in python

specialscope picture specialscope · Jul 19, 2012 · Viewed 412.5k times · Source

In python say you have

s = "string"
i = 0
print s+i

will give you error so you write

print s+str(i) 

to not get error.

I think this is quite a clumsy way to handle int and string concatenation. Even Java does not need explicit casting to String to do this sort of concatenation. Is there a better way to do this sort of concatenation i.e without explicit casting in Python?

Answer

user647772 picture user647772 · Jul 19, 2012

Modern string formatting:

"{} and {}".format("string", 1)