I can't put an integer on a string

Asked 2 years ago, Updated 2 years ago, 158 views

I can't put an integer on the string, so what should I do?

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

for i in range (1,10):
  string="string"+i

string python integer concatenation

2022-09-21 15:58

1 Answers

Cannot perform + operation because the two are of different types. Therefore, instead of an integer i, str(i) should be a string.

Last result, string is

for i in range (1,10):
    string="string"+str(i)
string = []
for i in range(1,10):
  string.append("string"+str(i))

#or
string = ["string"+str(i) for i in range(1,10)]


2022-09-21 15:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.