Plus Equals

Can you use the += and add multiple variables (example - print(“The total price is”, new_sneakers += nice_sweater += fun_books)

Hello, @raydude6131, and welcome to the Codecademy Forums!

+= is an assignment operator, and cannot be used within a call to the print function. The arguments passed to a function must be expressions. Inclusion of that assignment operator would create a statement, which does not qualify as an expression.

However, you can use the += operator to add the value of an expression containing multiple variables to the value of the variable to the left of that assignment operator.

Edited on January 1, 2020 to add this example:

Following is an example:

a += b + 3 * c + d

That adds the value of b + 3 * c + d to the value of a and saves the result to a.

Edited on January 2, 2020 to add the following:

In the case of keyword arguments, the = operator can be included as part of a function call. See print(*objects, sep=’ ', end=’\n’, file=sys.stdout, flush=False).

Here is an example:

print("Hello", "World", sep=", ", end="!")


Hello, World!
1 Like