I can't put an integer on a string

Asked 1 years ago, Updated 1 years ago, 140 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.