This question already has an answer here:
I want to create string using integer appended to it, in a for loop. Like this:
for i in range(1,11):
string="string"+i
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?
To get
string0, string1 ..... string10
, you could do likeIf we want output like
'string0123456789'
then we can usemap function
andjoin
method of string.If we want List of string values then use
list comprehension
method.Note:
Use
xrange()
for Python 2.xUSe
range()
for Python 3.xNOTE:
The method used in this answer (backticks) is deprecated in later versions of Python 2, and removed in Python 3. Use the
str()
function instead.You can use :
It will print
string012345678910
.To get
string0, string1 ..... string10
you can use this as @YOU suggestedUpdate as per Python3
You can use :
It will print
string012345678910
.To get
string0, string1 ..... string10
you can use this as @YOU suggested