In the context of this exercise, how does string concatenation work?
Answer
In string concatenation, each time a string is added, Python allocates memory to fit the total string, and then copies all the strings into that location in memory. So, if you were adding multiple strings together, like
string1 + string2 + ... + stringN
It would do this process during each concatenation, always adding from the very first string each time
string1 + string2 string1 + string2 + string3 ...
until it reaches the last added string.
As a result, concatenation with many strings can be inefficient, but for most problems that we will encounter, the time it takes will usually be negligible.
Whilst you could use loops there’s a built-in string method for this string.join() which is probably the most effective way to do it. I’d guess that it will pop up in later lessons but if not there are some relevant FAQs on the forums-
Hm, I was able to work out the code I needed to get it to pass (I believe), minus “defining” the message. I seem to be getting tripped up on that. Any advice?
Python is script and does not need to be compiled. That happens in real time when we Run the script. Are you working in a lesson, or in your own environment?
The title for this topic includes "string concatenation" in the name. That is the term we use to describe the combining of two or more objects to form a single string.
Concatenation uses the plus sign operator, + between the objects we wish to combine.
a = 42
b = "The meaning of life is, " + str(a) + "."
print (b) # The meaning of life is, 42.