0

why can't we write >>>4*'2' as 44 rather than 2222

24th Jun 2017, 5:13 AM
Jeevita Shetty
Jeevita Shetty - avatar
3 Respostas
+ 3
'4'*2
24th Jun 2017, 5:44 AM
Hetbo.net
Hetbo.net - avatar
+ 3
Because in your example, 4 is a literal integer value (not quoted) and '2" is a literal sring value, so this is implicit to result 2222 ^^ (as inverse mean nothing... number of repetition need to be an integer, and 'repetition' of this kind only apply to string, not number) As suggested by @HelboTeam, to get 44, do the inverse (4 enclosed in quotes, and 2 not): '4'*2
24th Jun 2017, 5:53 AM
visph
visph - avatar
0
In Python, the multiplication operator (*) is overloaded. When "multiplying" an integer by a string, the answer is that string repeated as many times as the integer value. So, 4 * '2' is the string '2' repeated 4 times or '2222'. Whereas, '4' * 2 is the string '4' repeated 2 times or '44'.
24th Jun 2017, 5:56 AM
Igor B
Igor B - avatar