michele michele - 1 year ago 103
Python Question

Python String and Integer concatenation

I want to create string using integer appended to it, in a for loop. Like this:

for i in range [1,10]:

But it returns an error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

What's the best way to concatenate the String and Integer?

Answer Source

You can use :

string = 'string'
for i in range(11):
    string +=`i`
print string

It will print string012345678910.

To get string0, string1 ..... string10 you can use this as @YOU suggested

>>> string = "string"
>>> [string+`i` for i in range(11)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download