Python print(): Output with sep and end
print() writes the values in the parentheses to the screen (standard output). If you give it several values separated by commas, it puts a space between them, and it adds a newline at the end. You can change both of these with sep and end.
print(value1, value2, ..., sep=" ", end="\n")
Printing several values at once
Values separated by commas are printed on one line with a single space between them. You can mix strings, numbers and booleans, and print turns them into text for you. print() with no values prints an empty line.
name = "Mina"age = 12print("name", name)print(name, age, 3.5, True)print()print("end")name Mina Mina 12 3.5 True end
sep: the text between values
sep is the text placed between one value and the next. The default is a single space (" "). Pass an empty string and the values are printed right next to each other.
print(2026, 11, 24, sep="-")print("a", "b", "c", sep="")print(1, 2, 3, sep=", ")print("x", "y", sep="\n")2026-11-24 abc 1, 2, 3 x y
sep is only used when there are two or more values. With one value there is nothing in between, so nothing happens.
end: the text added at the very end
end is the text added at the very end, after everything has been printed. The default is a newline ("\n"), so each print makes one line. With end="" the line does not break, so the next print continues on the same line.
print("A", end="")print("B", end="")print("C")for i in range(3): print(i, end=" ")print()print("next line")ABC 0 1 2 next line
If you print with end=" " in a loop, you need an empty print() after the loop to end the line.
Unpacking a list onto one line
If you print a list as it is, the square brackets and commas are printed too. Put * in front and the values are unpacked and passed one by one, so you get one line separated by spaces. You can use sep with it as well.
nums = [3, 1, 2]print(nums)print(*nums)print(*nums, sep=", ")print(*"abc")[3, 1, 2] 3 1 2 3, 1, 2 a b c
Common mistakes
Joining a string and a number with + to print them
age = 12
print("age: " + age)
→ TypeError: can only concatenate str (not "int") to str
+ only joins strings to strings. Separate the values with a comma, as in print("age:", age), or convert with str(age) before joining.
Storing the result of print in a variable
x = print("hi")
print(x)
→ hi is printed, and x holds None
print only writes to the screen and returns no value. To use a value later, store it in a variable first (x = "hi") and then print that variable.
Printing with end=" " and not ending the line
for i in range(3):
print(i, end=" ")
print("done")
→ done is not on the next line but follows on the same line, and a space is left after 2
Add one print() after the loop to end the line. To avoid the trailing space, it is easier to unpack and print everything at once, as in print(*range(3)).
Leaving out the parentheses
print "hi"
→ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
print "hi" without parentheses is Python 2 syntax. In Python 3 you always need the parentheses, as in print("hi").