Reputation: 39
I'm trying to figure out why my code doesn't work when I'm trying to print a collection of strings. In Python 2 I could usually do:
print ('test is') + ('this')
However, in Python 3 it prodcues the following error:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
I have tried replacing '+' with 'and' which works but the string doesn't come out after that. Can someone please explain to me how and why?
Upvotes: 1
Views: 80
Reputation: 135
If you want to print two string in Python 3 then you need to contain them all in one bracket, print (('test is ')+('this'))
, so that it's printed all as one string instead of adding print('test is ')
(nonetype) to ('this')
(string).
Upvotes: 0
Reputation: 1859
In Python 3 print
is not a statement unlike Python 2. It is a function. Moreover that function returns a NoneType
.
So when you type print ('test is') + ('this')
in Python 3.x, you are trying to add NoneType
to str
and hence the error.
The correct thing ( I expect you want to do this) is to type :
print('test is' + 'this')
Upvotes: 4