Show results on the screen with print(), and control exactly what it writes.
# simple print
print("Hello, World!")
# ans: Hello, World!
print("Python")
# ans: Python
The ↵ marks the newline print adds at the end of every call.
# multiple arguments
print("Age:", 25)
# ans: Age: 25
print("Name:", "Alice", "Score:", 95)
# ans: Name: Alice Score: 95
Separate arguments are joined with a space by default.
# custom separator
print("a", "b", "c", sep="-")
# ans: a-b-c
print(1, 2, 3, sep=" | ")
# ans: 1 | 2 | 3
# custom end character
print("Hello", end="")
print("World")
# ans: HelloWorld
print("Line1", end=" ")
print("Line2")
# ans: Line1 Line2
end replaces the newline, so the next print continues on the same line.
# print with expression
print(5 + 3)
# ans: 8
x = 10
print("x =", x)
# ans: x = 10
Python works out the expression first, then prints the result.
Say exactly what appears on screen, then press →.
# what is printed?
print(1, 2, 3)
# ans: 1 2 3
# what separator is used?
print("a", "b", "c", sep="-")
# ans: a-b-c
# what is printed?
print("Hello", end="")
print("World")
# ans: HelloWorld
# multiple arguments?
print(5 + 3)
# ans: 8
# what does this print?
print("Result:", 10, 20, sep=", ")
# ans: Result:, 10, 20
# what about this?
print()
# ans: (empty line)
# print with expression?
x = 5
print("x =", x)
# ans: x = 5
# what is the output?
print("Line1\nLine2")
# ans: Line1
# ans: Line2 (on separate lines)
# tab character?
print("Name:\tAlice")
# ans: Name: Alice (with tab)
# escape quotes?
print("He said \"Hi\"")
# ans: He said "Hi"
# raw string?
print(r"C:\new\path")
# ans: C:\new\path (backslashes not escaped)
# print return value?
x = print("Hello")
# ans: Hello (printed)
# ans: x is None
# empty print?
print("")
# ans: (empty line)
# multiple newlines?
print("A\n\nB")
# ans: A
# ans: (blank line)
# ans: B
sepGoes between the values; a space by default.
endGoes after the last value; a newline by default.
\n is a newline, \t a tab, \" a quote; an r"..." string keeps backslashes as they are.
It shows text; it does not give a value back.