What are the differences and uses of comma (,) and str() function in a print statement

Are there any differences in using a comma and str() function in a print statement? What is the primary use of the str() function if the console will still display the result when I use the comma over the str() function?

Link to the lesson: https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-syntax/exercises/concatenation

Let’s not mix things up. A print() statement is, under the hood, a very complex function. So much is hidden away (abstracted) we have no idea how much work is being done. Take for instance the comma that so many are fixated on as a form of concatenation. To be sure, it is not concatenation! It is consecutive print operations with a space character between rather than a newline.

Concatenation is the joining of two strings. Period. It is an operation that results in a single string of characters. This operation cannot be done without the operator.

Its use is to attempt a string interpretation of whatever argument we give it. It also follows that if we attempt to concatenate a non-string object it will raise an exception, hence the need re-cast the object to a string representation. The str() function is our tool, in that instance.

4 Likes

Thank you for this fast response :smiley: Now I understand its usage. I had another question in mind that was about the point of using the str() function if I write a code, say a = 5 and b = 1, so instead of using the str() function, why can’t I just assign a string value to the specified variable, a = "5"' and b = "1".

So I tested it out in Python interpreter and remember that the str() function will join these two values together, or simply concatenate them. Therefore, the result was 51 (or 5 1 when using a comma) instead of 6.

I apologize if I might’ve thought wrong and even tested it wrongly :grimacing:

The reason you don’t get 6 from doing print(a+b) where a and b are strings is because to the compiler, 5 is just another string. If you have a+b where a and b are numbers, then the compiler treats them as such.

1 Like

Because that is not the data type we need them to be in order to perform arithmetic operations. Even in terms of memory, a number is cheaper than a string.

https://code.tutsplus.com/tutorials/understand-how-much-memory-your-python-objects-use--cms-25609

A number takes 24 bytes to store. A string takes at the very least, 37 bytes, for an empty string! It is never advisable to fudge data type just to accommodate an operation. That is why we have a built in to work with number conversion to string output.

1 Like

I thought right then, thank you :smiley:

1 Like

I’ve never really taken the memory usage into account when I write my code in this way, I’m still sorta new to programming so when my codes work, even though it takes ages to compile, I’m kind of just happy that it works :grimacing:

But I believe I’ll be learning about ways to reduce memory usage down the line in this Python course. Also, I tested the memory usage of a number and a string using Python interpreter from the link you’ve provided and it turns out that it took 50 bytes for a string.

>>> sys.getsizeof("5")
> 50

again, thank you @mtf for this quick response with clear explanations and clarifications to my questions and confusion.

1 Like