So far, every program has produced output through print(). In this lesson we’ll look at print() in more detail — and meet input(), the function that reads a line from the keyboard.
print()
print() writes one or more values to the terminal, then moves to a new line:
print("Hello")
print("World")
Hello
World
Printing multiple values
You can pass several values, separated by commas. print() joins them with a space:
name: str = "Python"
year: int = 1991
print("Hello,", name, "version", year)
Hello, Python version 1991
Custom separator and end
print() has two useful keyword arguments — sep (between values) and end (after the last value):
print("a", "b", "c") # a b c
print("a", "b", "c", sep="-") # a-b-c
print("a", "b", "c", sep="") # abc
print("Hello", end="")
print("World")
HelloWorld
By default end is "\n" (a newline). Setting end="" lets you continue printing on the same line.
Mixing types
You can print numbers, strings, lists — anything. print() converts each value to a string automatically:
items: list[int] = [1, 2, 3]
print("Items:", items, "Total:", sum(items))
Items: [1, 2, 3] Total: 6
f-strings vs commas
For anything more than a few values, an f-string is cleaner:
name: str = "Manikandan"
score: int = 95
# OK
print("Name:", name, "Score:", score)
# better
print(f"Name: {name}, Score: {score}")
input()
input() pauses the program, waits for the user to type something and press Enter, then returns whatever they typed as a string:
name: str = input("What's your name? ")
print(f"Hello, {name}!")
What's your name? Manikandan
Hello, Manikandan!
The argument to input() (the prompt) is optional. It’s shown to the user but not added to the returned value.
input always returns a string
Even if the user types digits, input gives you a string. You have to convert it yourself:
text: str = input("How old are you? ")
age: int = int(text)
print(f"Next year you'll be {age + 1}")
If the user types something that isn’t a number, int(text) will raise a ValueError. We’ll handle that properly in Section 9.
A small interactive program
Let’s combine everything:
name: str = input("Name: ")
age_text: str = input("Age: ")
age: int = int(age_text)
if age >= 18:
print(f"Welcome, {name}. You can vote.")
else:
years_left: int = 18 - age
print(f"Hi {name}, you can vote in {years_left} year(s).")
Run it:
uv run python greet.py
Name: Manikandan
Age: 16
Hi Manikandan, you can vote in 2 year(s).
We’re using if/else here without having properly covered it — that’s the next section.
input() in real ML programs
You’ll rarely use input() in a serious ML or data project. Real workflows read data from files, command-line arguments, or APIs — not the keyboard. But input() is the simplest way to make small interactive programs while you’re learning, so we’ll use it for examples.
Printing to standard error
There’s a less-known cousin of print() for error messages:
import sys
print("This goes to standard output")
print("This goes to standard error", file=sys.stderr)
The difference matters for tools and scripts that pipe data — error messages go to stderr so they don’t get mixed into the data stream. We’ll touch on this again in Section 14.
What’s next
You can put values into the program and get them out. That completes Section 2.
Next up: Operators and Expressions — the symbols that combine values to compute new ones.