The print() Function
What You'll Learn
- How
print()works and its different uses - Printing multiple values with commas
- Customizing the separator with
sep - Controlling line endings with
end - Printing multiple lines and blank lines
1. The Basics of print()
You have already been using print() since Module 1. Now let us explore its full power. At its simplest, print() takes a value and displays it:
print("Hello, World!") print(42) print(3.14) print(True)
You can also print the value of variables:
name = "Alice" age = 25 print(name) print(age)
2. Printing Multiple Values
You can pass multiple values to print() separated by commas. Python will automatically insert a space between each value:
name = "Alice" age = 25 print("Name:", name) print("Age:", age) print("Hello,", name, "you are", age, "years old.")
Why Commas Are Easier Than Concatenation
When using commas, Python automatically converts each value to a string and adds spaces. With concatenation (+), you must convert manually:
# With commas (easier!) print("Score:", 95) # With concatenation (must convert manually) print("Score: " + str(95))
You can print as many values as you want, mixing different types freely:
print("Name:", "Alice", "| Age:", 25, "| GPA:", 3.85, "| Active:", True)
3. The sep Parameter
By default, print() separates multiple values with a space. You can change this using the sep (separator) parameter:
# Default separator (space) print("apple", "banana", "cherry") # Custom separators print("apple", "banana", "cherry", sep=", ") print("apple", "banana", "cherry", sep=" - ") print("apple", "banana", "cherry", sep="\n")
Practical Use: Formatting Dates and Times
month = 3 day = 15 year = 2025 print(month, day, year, sep="/") hours = 9 minutes = 30 print(hours, minutes, sep=":")
You can even use an empty string as a separator to join items with no space:
print("2", "0", "2", "5", sep="")
4. The end Parameter
By default, print() adds a newline character (\n) at the end, which is why each print() appears on a new line. You can change this behavior with end:
# Default behavior - each on a new line print("Hello") print("World") print() # blank line # Using end to keep printing on the same line print("Hello", end=" ") print("World")
Building Output on One Line
# Countdown on one line print("3", end="...") print("2", end="...") print("1", end="...") print("Go!")
You can use sep and end together:
print("a", "b", "c", sep="-", end="!\n") print("Done.")
5. Printing Blank Lines and Multi-Line Output
Calling print() with no arguments prints a blank line:
print("Section 1") print() print("Section 2")
You can also use the \n escape character within a string to create multi-line output from a single print() call:
print("Line 1\nLine 2\nLine 3")
6. Putting It All Together
# A formatted student report name = "Maria Garcia" student_id = 12345 grade = 92.5 print("=" * 30) print("STUDENT REPORT") print("=" * 30) print("Name:", name) print("ID:", student_id) print("Grade:", grade) print("=" * 30)
Check Your Understanding
Question 1: What is the output of print("a", "b", "c", sep="*")?
Answer: a*b*c -- the sep parameter replaces the default space with *.
Question 2: How would you print "Hello" and "World" on the same line using two print() calls?
Answer: print("Hello", end=" ") followed by print("World"). The end=" " replaces the newline with a space.
Try It Yourself!
- Print today's date in MM/DD/YYYY format using
sep. - Use
endto print the numbers 1 through 5 on a single line separated by dots. - Create a formatted box around your name using
print()and string repetition.
Key Takeaways
print()displays values to the console. It automatically converts values to strings.- Use commas to print multiple values (Python adds spaces automatically).
sepcontrols the separator between multiple values (default is a space).endcontrols what appears at the end (default is\nnewline).print()with no arguments prints a blank line.