Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,365 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?