Why should we use concatenation instead of just the print() function?

Question

The print() function can concatenate for us if strings are given as arguments, why not just use this instead of concatenating with +?

Answer

To answer this let’s start by looking at the print function (for more details about + concatenation check the other FAQs). Here you can see the full documentation for the print function (at time of writing python 3.8 is the latest). So looking at the print function:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
You can see that there are a few arguments here, we are going to only look at *objects, and sep=' ' for the purposes of this lesson. To quote the documentation " Print objects to the text stream file , separated by sep."
The argument *objects, is any number of different objects that will all be printed together, for example our list of string1, string2, string3, etc.. Usefully, any non string objects will be converted by default using str(), unlike if we used concatenation. And the seperator is an argument that for a string that is placed between each object. By default it is a single space ' ', but you could change it if you wished for example by setting sep=', ', now there is a comma then a space.

So far this seems more useful and easier than concatenation, but there is a catch. The separator is placed between all objects. This means we cannot mix and match. For example:

string1 = 'My favorite number'
num = 14
string2='is the best number'
print(string1+': '+str(num)+' '+string2)
>My favorite number: 14 is the best number

So concatenation is going to be more useful overall for putting together custom sentences with variables while the print function is going to be more useful for “stringing” together similar types of objects (words in a sentence, numbers in a list, etc.)

I hope this helps!

Hi all :slight_smile:

So there’s two ways to print a concatenated string with a number in it:

  1. Make the number a string first with the str() function, then print(it + with + other + stuff)
  2. Don’t do 1, just print(it, inside, this, function)

What’s the reason behind these two methods? Why use 1) and not 2) ?

5 Likes

should be more simple?

4 Likes

I guess you could put all of the strings into a variable to put inside the print() function…
Not really more simple, just easier to understand though.

@py1184336943

Look at the difference between the spacing of your solution and the “print(message)” solution. I just started and had the same thought, but I noticed right away that it prints the spaces between each string differently. I wonder if a later lesson covers that wider space.

1 Like

I think this is because when you use print in this way it adds a space after each argument. There are already spaces included in the test string, like here: "The Wind, " - the result of this is a double space after each test string. You could test this by removing the spaces from the text string, like so: “The Wind,” instead of "The Wind, " and so on

2 Likes

Yes, because as @catower explained in his answer to another question above, the default separator for the print() function is a space.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
That’s what sep=’ ’ means above.

Just like one of the benefits of using concatenation is customizing the outputted string, one of the benefits of passing arguments to print() with commas is it will automatically add the space for you. (Or a ‘,’ or a ', ’ or a ‘;’ depending on however you want the arguments to be separated.)