Printing and formatting
Print several values on one line, and choose what goes between them and after them.
- Print a label and a value with one
print - Change what goes between values with
sep= - Change the line ending with
end=, and use\nfor a new line
print can take several values, separated by commas. It prints them on one line with a single space between each. A label and a number are just two values.
Run each example to see what it prints. Running examples doesn't count toward progress.
print("Order", "O100043", "is", "delivered") print("Items:", 1, "Total:", 12.93)
Two settings change that spacing. sep= is what goes between the values. end= is what goes after the last value. Normally that is a new line, so the next print starts on the next line. end="" puts nothing after the value, so the next print continues on the same line.
Inside quotes, \n means new line. You type two characters, a backslash and an n, and Python starts a new line there. It works with sep and with end.
print("2025", "11", "28", sep="-") print("Status", end=": ") print("delivered") print("toys", "electronics", sep="\n")
When to use them: sep= when several values belong on one line with something other than a space between them (a date, a route, a table row); end= when the next print should continue the same line.
Mistakes to avoid: sep and end are easy to swap: end="-" puts one dash after the last value and leaves spaces between the values. The settings go inside the brackets, after the values. And /n with a forward slash is just two characters; a new line is a backslash: \n.
- Several values in one
printare joined by spaces. The comma is not printed. sep=sets what goes between values.end=sets what goes after the last value.\ninside quotes is a new line.