Lesson 6 of 25

User Input & Output

The print() Function

The print() function is Python's primary way to display output to the console. It can accept multiple arguments, which it prints separated by spaces by default.

You can customize print() behavior using the 'sep' parameter (separator between arguments) and the 'end' parameter (what to print at the end instead of a newline).

Example
# Basic printing
print("Hello, World!")

# Multiple arguments
print("Name:", "Alice", "Age:", 25)
# Output: Name: Alice Age: 25

# Custom separator
print("2025", "03", "29", sep="-")
# Output: 2025-03-29

# Custom end character
print("Loading", end="")
print("...", end="")
print(" Done!")
# Output: Loading... Done!

# Printing special characters
print("Line 1\nLine 2")   # Newline
print("Tab\there")         # Tab
print("She said \"hi\"")  # Escaped quotes
  • print() adds a newline at the end by default
  • sep=' ' — default separator between multiple arguments is a space
  • end='\n' — default ending is a newline; change it to print on the same line
  • \n = newline, \t = tab, \\ = backslash, \" = quote
Try print() Options
JavaScript
# Try different print options
print("Hello, World!")
print("Name:", "Alice", "Age:", 25)
print("2025", "03", "29", sep="-")
print("Loading", end="...")
print(" Done!")
Notes
  • In Python 3, print() is a function and requires parentheses. In Python 2, print was a statement (print 'hello'), which is no longer valid syntax.

The input() Function

The input() function pauses the program and waits for the user to type something and press Enter. It always returns the user's input as a string, so you need to convert it if you want a number.

You pass a prompt string to input() to display a message before the cursor, telling the user what to type.

Example
# Basic input
name = input("What is your name? ")
print(f"Hello, {name}!")

# Input always returns a string — must convert for numbers
age_str = input("How old are you? ")
age = int(age_str)  # Convert string to integer
print(f"You will be {age + 1} next year.")

# You can convert inline
height = float(input("Enter your height in meters: "))
print(f"Your height is {height}m")

# Simple calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print(f"{num1} + {num2} = {num1 + num2}")
  • input() always returns a string, even if the user types a number
  • Use int() to convert input to an integer
  • Use float() to convert input to a decimal number
  • Always validate user input in real programs to avoid errors
Try User Input
JavaScript
# This code requires terminal input
# Try running it in your Python IDE
name = input("What is your name? ")
age = int(input("How old are you? "))
print(f"Hello, {name}!")
print(f"You will be {age + 1} next year.")
Notes
  • If the user enters non-numeric text when you try to convert with int() or float(), Python will raise a ValueError. In a later lesson, you'll learn to handle this with try/except.