Adding a break while updating a variable with strings

When updating a variable using “+=” to tack on additional strings, how do you add a break between said strings?

I’m doing a module that has me add descriptions of products to an itemized list, but when I add multiple descriptions they appear as two sentences one after another, rather than one below the other. I looked up how to add a break, but I get an invalid syntax error if I try to include “+ /n +”.

Module for reference:
https://www.codecademy.com/courses/learn-python-3/projects/python-furniture-store

Edit: For example if I have
item1 = “Milk”
item2 = “Eggs”
shopping_list = “”

shopping_list += item1 + " " + item2
print(shopping_list)

Then the result is :
Milk Eggs

But I’d like it to be:
Milk
Eggs

If that makes sense.

use + "\n" to add a line break
use + " " to add a space
use + "\t" to add a tab character

to do what you wanted (each item on its own line):
shopping_list += item1 + "\n" + item2

1 Like

Oh! I’ve gotta put \n in quotations to treat it as a string, thank you! I probably should have realized that, my bad.

Something of note that one cannot unlearn is, f-strings. We can write one string and embellish it with interpolation syntax.

shopping_list = f"{item1}\n{item2}"
2 Likes