FAQ: Learn Python: Syntax - Concatenation

This community-built FAQ covers the “Concatenation” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Concatenation

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: What is an argument in python?

4 posts were split to a new topic: Why don’t I see any strings?

A post was merged into an existing topic: Is there a way to enter all the variable names with code instead of by hand?

2 posts were merged into an existing topic: Why should we use concatenation instead of just the print() function?

A post was split to a new topic: Does the console automatically wrap text?

A post was split to a new topic: Why didn’t this number need to be a string?

A post was split to a new topic: Why didn’t my code work?

2 posts were split to a new topic: Is there a way to enter all the variable names with code instead of by hand?

A post was split to a new topic: Why should we use concatenation instead of just the print() function?

Instead of writing age = 20 and then proceeding to code print(birthday_string + str(age)), can’t we instead write: age = “20” and then print(birthday_string + age)? If we just have to add quotes to the number, then why is str needed?

I’m a bit confused. On the instructions it states how a comma ( , ) and a plus ( + ) can be used interchangeably when doing strings in common cases as the one in the module. However, I get different results when using them in the example exercise. Can someone explain why this happens?

With Plus:

string1 = 'The wind ’

string2 = 'which had hitherto carried us along with amazing rapidity, ’

string3 = 'sank at sunset to a light breeze; ’

string4 = 'the soft air just ruffled the water and ’

string5 = 'caused a pleasant motion among the trees as we approached the shore, ’

string6 = ‘from which it wafted the most delightful scent of flowers and hay.’

message = string1 + string2 + string3 + string4 + string5 + string6

print(message)

Result:

The wind which had hitherto carried us along with amazing rapidity, sank at sunset to a light breeze; the soft air just ruffled the water and caused a pleasant motion among the trees as we approached the shore, from which it wafted the most delightful scent of flowers and hay.

With Commas:

string1 = 'The wind ’

string2 = 'which had hitherto carried us along with amazing rapidity, ’

string3 = 'sank at sunset to a light breeze; ’

string4 = 'the soft air just ruffled the water and ’

string5 = 'caused a pleasant motion among the trees as we approached the shore, ’

string6 = ‘from which it wafted the most delightful scent of flowers and hay.’

message = string1, string2, string3, string4, string5, string6

print(message)

Result:

('The wind ', 'which had hitherto carried us along with amazing rapidity, ', 'sank at sunset to a light breeze; ', 'the soft air just ruffled the water and ', 'caused a pleasant motion among the trees as we approached the shore, ', ‘from which it wafted the most delightful scent of flowers and hay.’)

What is the link to the lesson? Is this Python2? You cannot use commas to concatenate that way.

One way to concatenate strings is with the + operator. You can use commas, but in a different way than your 2nd example:

x = "Hello"

y = "World!"

print(" ".join([x, y]))

>>Hello World!

I completed the Hello World Concatenation exercise using comma concatenation for the six provided strings, however the quotes in the string variables were taken literally and presented in the output.

string1 = "The wind, "
string2 = "which had hitherto carried us along with amazing rapidity, "
string3 = "sank at sunset to a light breeze; "
string4 = "the soft air just ruffled the water and "
string5 = "caused a pleasant motion among the trees as we approached the shore, "
string6 = "from which it wafted the most delightful scent of flowers and hay."

# Define message below:
message = string1, string2, string3, string4, string5, string6
print(message)
#prints: ('The wind, ', 'which had hitherto carried us along with amazing rapidity, ', 'sank at sunset to a light breeze; ', 'the soft air just ruffled the water and ', 'caused a pleasant motion among the trees as we approached the shore, ', 'from which it wafted the most delightful scent of flowers and hay.')

I resorted to using the following code to comma concatenate the six strings, and the output was presented correctly without quotation marks.

message = "".join([string1, string2, string3, string4, string5, string6])
print(message) #prints: The wind, which had hitherto carried us along with amazing rapidity, sank at sunset to a light breeze; the soft air just ruffled the water and caused a pleasant motion among the trees as we approached the shore, from which it wafted the most delightful scent of flowers and hay.

In reference to my original code, could someone explain why the quotation marks were taken from the defined variables and output?

Thank you in advance.

Commas are separators not, operators. There is no such thing as ‘comma concatenation.’ Just so we’re clear.

str.join(list) is a string method acting upon a list object. It has nothing to do with concatenation., so we’re clear.

There is only one concatenation operator, and only one literal form of concatenation, the literal appending of one string object to another, or in the case of Python, one list/tuple to another. That’s the spread of operations involved in concatenation.

Granted, a print() operation may generate output that looks like it has be concatenated, but that is not the case. It is merely content that has been sent to the screen with no line breaks between arguments, only a space separator.

Bottom line, if there is no, + sign between two string objects, or two list/tuple objects, it is not concatenation in the literal sense.


So we’re clear, we cannot apply concatenation to dictionary objects…

{'a': 'b'} + {'c': 'd'}
Traceback (most recent call last):

  Cell In[35], line 1
    {'a': 'b'} + {'c': 'd'}

TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

Nor can we use the operator on set objects:

{1, 2, 3} + {4, 5, 6}
Traceback (most recent call last):

  Cell In[36], line 1
    {1, 2, 3} + {4, 5, 6}

TypeError: unsupported operand type(s) for +: 'set' and 'set'
1 Like

Thank you mtf for your detailed response, noted!

1 Like