Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (47.6k points)

I want to create a string using integer appended to it, in a for a 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?

1 Answer

0 votes
by (106k points)

For creating a string using integer appended to it, in a for loop we have a method which is str().

You can use the below-mentioned code:-

string = 'string' for i in range(11): 

string +=`i` 

print(string)

image

If you want to get string0, string1 ..... string10 you can use the following code:-

string = "string" 

[string+`i` for i in range(11)]

image

If you are a Python 3 user then you can use the following code:-

string = 'string' for i in range(11):

string +=str(i)

print(string)

image

And for getting string0, string1 ..... string10 you can use the below-mentioned code:-

string = "string" 

[string+str(i) for i in range(11)]

image

Related questions

0 votes
1 answer
asked Jul 26, 2019 in Java by Shubham (3.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...