Why error in first?and not in the second one? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why error in first?and not in the second one?

A=3 print('mom'+A) Traceback (most recent call last): File "source_file.py", line 2, in <module> print('mom'+A) TypeError: can only concatenate str (not "int") to str A=3 print('mom'*A) Output:mommommom

2nd Jul 2019, 4:13 PM
Anurag aghara
Anurag aghara - avatar
1 Answer
+ 2
3 is not a string, but a number. In Python you can not add a number to a string, because it's not defined. If you want to do it, you'd first have to convert the number to a string: print('mom' + str(A)) * on the other hand is defined for strings: It means 'gimme A times that string'.
2nd Jul 2019, 4:19 PM
HonFu
HonFu - avatar