Kudu Kudu - 1 year ago 87
Python Question

Making a string out of a string and an integer in Python

I get this error when trying to take an integer and prepend "b" to it, converting it into a string:

File "program.py", line 19, in getname
name = "b" + num
TypeError: Can't convert 'int' object to str implicitly

That's related to this function:

num = random.randint(1,25)
name = "b" + num

msw msw
Answer Source
name = 'b' + str(num)


name = 'b%s' % num

as S.Lott notes, the mingle operator '%' is deprecated for Python 3 and up. And I stole the name "mingle" from INTERCAL but that's how I talk about it and wanted to see it in print at least once before - like the dodo - it vanishes from the face of the earth.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download