nums = [1,2,3] [str(num)+ 'Hello' for num in nums] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

nums = [1,2,3] [str(num)+ 'Hello' for num in nums]

how does the str(num)+ work here?

31st Jul 2021, 6:39 AM
Dipanjan Basu
Dipanjan Basu - avatar
2 Answers
+ 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
31st Jul 2021, 6:56 AM
Abhiyantā
Abhiyantā - avatar
+ 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'.
31st Jul 2021, 6:53 AM
Tibor Santa
Tibor Santa - avatar