Difference between + and , in python3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Difference between + and , in python3

I'm a little confused, if you try: variable = 10; print(variable, "hello!"); the code Will run, why? if you try this: variable = 10; print(variable + "hello!"); I will get an error, can someone explain me the difference between using + and ,?

3rd Apr 2018, 6:42 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
3 Answers
+ 7
The comma separates the results and print function prints them both. n = 10, h = 'hi' print(h, 'ho') -> hi ho The add "+" operator can add two numbers or concat strings but not number and string print(n + 20) -> 30 print('2' + '3N') -> 23N print(n + 'hi') #error
3rd Apr 2018, 6:54 AM
Lord Krishna
Lord Krishna - avatar
+ 3
operator + is not defined for int,string. But print(a,b,c....) is independent of type of a,b,c.... the system looks up the appropiate representation of the type.
3rd Apr 2018, 6:56 AM
Oma Falk
Oma Falk - avatar
0
using + is a math operator. it wants to add numbers and you are only giving it 1 number. the comma is for concatenation. it just places whatever you want on the line. you could convert the number to a string like this... str(variable) or variable = str(10) then you can use the plus sign. Also when you use a comma it will automatically add a space. The plus won't. Hope this helps.
3rd Apr 2018, 6:55 AM
synorax
synorax - avatar