If I use:
>>> n = input("What is your name? ")
What is your name? 23
>>> print("Hello " + n)
The Output is
Hello 23
Then it prints correctly. But if I use:
>>> n = 1
>>> print("Hello" + n)
Result:
Traceback (most recent call last): File "<pyshell#74>", line 1, in
<module>
print("Hello" + n) TypeError: can only concatenate str (not "int") to str
Then it gives an error.
I know that it is not allowed to print numbers with string without using the str()
function, so in the first case why does it not give an error?
Because
input()
always returns astr
.In your first example,
n
is"23"
, not23
, so the concatenation works.