What makes you think +
should behave in the same way for two different languages? In Java, when you have something like
System.out.println(5 + "5");
55
string concatenation is performed, as you can see. This is outlined by the Java language's specification (see JLS §15.18.1). By contrast, Python doesn't coerce the types like Java (hence the TypeError
); in order for string concatenation to occur, both operands of +
must be strings, which is why you need
>>> "5" + str(5)
'55'
This is outlined in Python's specification (see Python Language Reference §5.6). There is no universal rule that states +
must behave in a specific manner across languages: if I wanted to, I could create a language in which +
performs subtraction!
As @Wooble points out, Perl's +
behaves in yet another way:
$ perl -e 'print 5 + "5"; print "\n"'
10