In this article, we will learn about the difference between + and, in Python print, The print() function in Python is used to print some messages as the output on the screen. We can print a single element or even multiple elements on the screen. Python provides two ways to print multiple elements as the output using the ‘+ operator’ and ‘, operator’. Let us see how they work with the print statement.
‘+’ in Python Print Statement
The ‘+’ operator is an arithmetic operator which is also used to concatenate two strings. It does not add white space in between strings, you have to provide it manually if you want to separate two strings. The print() function, can be used to combine multiple strings and print them to the console as a single line.
Syntax of ‘+’ Operator in Python Print:
print(string 1 + string2)
Example 1:
In this example, we can see that we did not provide any white space in the string. So when we try to print them using the + operator, string 1 and string 2 concatenate with each other.
Python
str1 = "Hello" str2 = "World" print (str1 + str2) |
HelloWorld
Example 2:
In this example, we can see that we provided a space between the strings – “Name” and “Age”. So when we printed them using the + operator, the white space was printed in the output as well. Also, it is used to concatenate strings, so we convert the age to string using the str() function.
Python
name = "Raj" age = 26 print ( "Name: " + name + ", Age: " + str (age)) |
Name: Raj, Age: 26
‘,’ in Python Print Statement
Python ‘,’ operator is used to separate multiple arguments passed to the print() function. When used with the print() function, it can be used to print multiple values or variables on separate lines. It automatically adds a single white space between the arguments.
Syntax of ‘,’ in Python Print:
print(arg1, arg2, arg3, …)
In this example, we can see that we did not provide any white space in the string, but the ‘,’ operator automatically added a single white space between the values.
Python
name = "Raj" age = 27 print ( "Name:" ,name) print ( "Age:" ,age) |
('Name:', 'Raj') ('Age:', 27)
Difference Between + and , in Python Print
Let’s see the difference between + and , in Python print.
‘ + ‘ in Python Print |
‘ , ‘ in Python Print |
---|---|
It is used to concatenate or add the values |
It separates multiple values or variables passed to the function |
Example: print(“Hello” + ” ” + “world”) would result in “Hello world” |
Example: print(“Hello”, “world”) would result in “Hello world” |
It does not add white space between the values |
It automatically adds a single white space between the values. |
It returns the new value that is the result of the concatenation or addition operation |
Does not return the value, but prints the values passed to the function to the console |
It is used for explicit string concatenation i.e, we need to convert non-string data types to strings using functions like str() if they are not already strings. |
Explicit string concatenation is not necessary. We can print a mix of data types without explicit type conversion. |