Если я пытаюсь сделать следующее:
things = 5
print("You have " + things + " things.")
Я получаю следующую ошибку в Python 3.x:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be str, not int
... и подобная ошибка в Python 2.x:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
Как я могу обойти эту проблему?
Другая альтернатива использует str.format()
метод для конкатенации интервала в Строку.
В Вашем случае:
Замена
print("You have " + things + " things.")
С [1 111]
print("You have {} things".format(things))
, если Вы имеете
first = 'rohit'
last = 'singh'
age = '5'
print("My Username is {}{}{}".format(first,age,last))