0
nums = [1,2,3] [str(num)+ 'Hello' for num in nums]
how does the str(num)+ work here?
2 ответов
+ 8
str() will convert your int to str, and when you use '+' between two str then it will concat like
>>> 'Hello' + ' World'
'Hello World'
>>> str(35)
'35'
>>> str(35) + 'Hello'
'35Hello'
>>> 35 + 'Hello'
TypeError: Unsupported oprand type for + 'int' and 'str'
So if you use print then it will print like
['1Hello','2Hello','3Hello']
https://code.sololearn.com/cl7O8ffhq0Md/?ref=app
+ 7
nums is a list that contains integer numbers.
In Python you cannot use + operator to add a number and a string. However you can concatenate two strings, so str(num) converts the number to a string, so then it can be combined with 'Hello'.