Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJacek Skrzypek
5,040 PointsString formatting in multiline print?
Hi, I wanted to print the notification like this:
You have added bread to your list.
You have 1 item(s) on your list.
but when I tried multiline print, the string formatting wouldn't work and the notification looked like this:
"You have added", item , "to your list,"
"you have {} items on your list".format(len(shopping_list))".
So, is there a simpler way to code it than using 'print' twice, like this?:
print("You have added", item , "to your list.")
print("You have {} item(s) on your list.".format(len(shopping_list)))
Peace, Jac
2 Answers
Steven Parker
231,269 PointsI'm not sure it is "simpler", but you can certainly do it with one "print" statement using a formatted (f) string:
print(f"You have added {item} to your list.\nYou have {len(shopping_list)} item(s) on your list.")
This would also work, and might be easier to read:
print(f"""You have added {item} to your list.
You have {len(shopping_list)} item(s) on your list.""")
Jacek Skrzypek
5,040 PointsGreat, thank you!