Python Introducing Lists Using Lists Mutability

Surbhi Anand
Surbhi Anand
1,515 Points

What is difference between print ("===>", Suggested_list, "<===") and print (books + "*") when to use "," and "+"?

there are instances when print statement is used with + sign to join two arguments. And sometimes, comma is used to join 2 arguments.

Once, i compiled a program where is used "," instead of "+" and print statement kept giving error.

Please let me know where to use "," and where to use "+" with Print ()

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,348 Points

Hi Surbhi,

The print statement with commas, uses a space to separate them. So using a comma will introduce a space automatically. A plus sign is used for string concatenation. I quote a nice stack overflow post here:

Concatenation creates each string in memory, and then combines them together at their ends in a new string (so this may not be very memory friendly), and then prints them to your output at the same time. This is good when you need to join strings, likely constructed elsewhere, together.

If you concatenate (+) stings you will need to provide a space yourself. Look at my example: Lets pretend we are in a REPL (open up a terminal (I am on a Mac machine) and type python

schleifers-imac:Desktop Grigorij$ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = "Surbhi Anand"
>>> activity = "learns pyhon"
>>> print(name, activity) # comma provides a space automatically
Surbhi Anand learns pyhon
>>> print(name + activity)
Surbhi Anandlearns pyhon # no space 
>>> print(name + " " + activity) # with extra space string
Surbhi Anand learns pyhon

Makes sense?