Why this is working.
Input print(9, "ABC")
Output 9 ABC
And This is not working and showing the error.
Input print(9 + "ABC")
output
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Why this is working.
Input print(9, "ABC")
Output 9 ABC
And This is not working and showing the error.
Input print(9 + "ABC")
output
TypeError: unsupported operand type(s) for +: 'int' and 'str'
9 is an integer and "ABC" is a string, so it is impossible to add an integer and a string.
You can use it like that;
print(str(9) + " " + "ABC")
The "+" symbol is used to concatenate two strings together. In your code 9 is and integer and "ABC" is a string, in order for the code to work, both items need to be strings, which means that you need to change print(9+"ABC")
to print(str(9)+"ABC")
+
operator can be used with same type of objects. In your case you are trying to add an Integer with a strings, which is a type mismatch.
In case you want to concatenate it as strings change 9
to String as in following statement:
print(str(9)+"ABC")
To answer why print(9, "ABC")
is working ?
This is working because print()
supports VLA (Variable length argument) and its considering 9
as a integer input where as "ABC"
as a String Input.