+ 2

Why these codes are different outputs?

print([i for i in range(0,3)]*'0') output=TypeError print(len([i for i in range(0,3)]*'0') output=000 why output are different,can anyone explain me.

5th Apr 2018, 4:32 PM
Maninder $ingh
Maninder $ingh - avatar
3 Answers
+ 2
you cannot multiply a list with string.
25th Apr 2018, 3:30 AM
Lokesh Kumar
Lokesh Kumar - avatar
+ 1
You can only use print("string"*int) to print repeats of a string. The first line is print("string"*list), which is why you get the type error. Are you wanting to print a list of increasing repeats of a string? That sounds fun. for i in range(3): print(i*'0')
5th Apr 2018, 4:48 PM
Nathan Hodges
Nathan Hodges - avatar
+ 1
I think you missing a ) in the second code sample... print(len([i for i in range(0,3)])*'0')
5th Apr 2018, 5:21 PM
Amir Galanty
Amir Galanty - avatar