Python Introducing Lists Build an Application Add Items

Jacek Skrzypek
Jacek Skrzypek
5,040 Points

String 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
Steven Parker
195,376 Points

I'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.""")