Error with print: unsupported operand type(s) for +: 'NoneType' and 'str'
Question:
I have code for concatenation of two string but it is showing me an error.
Here is the code :
Name = "Praveen kumar"
print (Name)+"Good boy"
Error message : unsupported operand type(s) for +: 'NoneType' and 'str'
How can I fix this?
Answers:
You are printing Name
and then adding the string Good boy
to it, you need to enclose your addition within the function call.
print(Name)
will return None
(it is a function which defines no return value) which is why you’re getting the unsupported operand...
error.
The code below will achieve what you want.
Name = "Praveen kumar"
print(Name + "Good boy")
However note that there will be no space between Name
and 'Good boy'
. If you want a space then you can use print(Name, "Good boy")
as the default separator is sep = ' '
, meaning that a space will be added between your arguments.
print
is a function, returning None.
So when you write
print(Name) + "Good boy"
You are actually adding the return value of the function call (i.e. None) to the string.
What you wanted instead was probably:
print(Name, "Good boy")
I have code for concatenation of two string but it is showing me an error.
Here is the code :
Name = "Praveen kumar"
print (Name)+"Good boy"
Error message : unsupported operand type(s) for +: 'NoneType' and 'str'
How can I fix this?
You are printing Name
and then adding the string Good boy
to it, you need to enclose your addition within the function call.
print(Name)
will return None
(it is a function which defines no return value) which is why you’re getting the unsupported operand...
error.
The code below will achieve what you want.
Name = "Praveen kumar"
print(Name + "Good boy")
However note that there will be no space between Name
and 'Good boy'
. If you want a space then you can use print(Name, "Good boy")
as the default separator is sep = ' '
, meaning that a space will be added between your arguments.
print
is a function, returning None.
So when you write
print(Name) + "Good boy"
You are actually adding the return value of the function call (i.e. None) to the string.
What you wanted instead was probably:
print(Name, "Good boy")