Learn Without Walls
← Module 3 Home Lesson 1 of 4 Next Lesson →

The print() Function

What You'll Learn

1. The Basics of print()

print(): A built-in Python function that displays text and values to the screen (the console). It is the primary way your program communicates with the user.

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)
Hello, World! 42 3.14 True

You can also print the value of variables:

name = "Alice"
age = 25
print(name)
print(age)
Alice 25

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.")
Name: Alice Age: 25 Hello, Alice you are 25 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))
Score: 95 Score: 95

You can print as many values as you want, mixing different types freely:

print("Name:", "Alice", "| Age:", 25, "| GPA:", 3.85, "| Active:", True)
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")
apple banana cherry apple, banana, cherry apple - banana - cherry apple banana cherry

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=":")
3/15/2025 9:30

You can even use an empty string as a separator to join items with no space:

print("2", "0", "2", "5", sep="")
2025

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")
Hello World Hello World

Building Output on One Line

# Countdown on one line
print("3", end="...")
print("2", end="...")
print("1", end="...")
print("Go!")
3...2...1...Go!

You can use sep and end together:

print("a", "b", "c", sep="-", end="!\n")
print("Done.")
a-b-c! 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")
Section 1 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")
Line 1 Line 2 Line 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)
============================== STUDENT REPORT ============================== Name: Maria Garcia ID: 12345 Grade: 92.5 ==============================

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!

  1. Print today's date in MM/DD/YYYY format using sep.
  2. Use end to print the numbers 1 through 5 on a single line separated by dots.
  3. Create a formatted box around your name using print() and string repetition.

Key Takeaways

← Module 3 Home Next: User Input →