How do you calculate this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you calculate this?

Hey guys. I know an answer can be really simple, but I don't get it at all. We have this print: print(int(3 * '3') + 2 * int('1' * 2)) and how the answer can be 355 and not 13? I know that when you multiply strings you just have to print them next to each other, but there are 'int' parts before parentheses and it should change the whole output into int. Am I wrong? My VSC also prints 355, so I need to know an answer how it's done. Cheers! :)

24th Feb 2023, 8:38 AM
Dawid Kapłon
Dawid Kapłon - avatar
3 Answers
+ 6
3 * '3' will be '333' (string repetition), so it will be int('333') – as an integer it will be 333. Similar, for the 2nd part of the expression: int('11'). So 11 is multiplied by 2. Eventually we get 333 + 2*11.
24th Feb 2023, 8:44 AM
Lisa
Lisa - avatar
+ 2
Oh ok, I got it. Thank You Lisa :)
24th Feb 2023, 10:44 AM
Dawid Kapłon
Dawid Kapłon - avatar
0
If you want to get this to 13: print((int('3') *3 ) + (2 * int('1') * 2))
25th Feb 2023, 1:30 AM
S3R43o3
S3R43o3 - avatar