Input & Output
Learn how to display output and get input from the user in Python.
Input & Output
Every useful program does two things — it shows information to the user, and it receives information from the user. In Python, this is done with two built-in functions: print() for output and input() for input.
print() — displaying output
print() sends text to the screen. You will use this constantly — for showing results, debugging, and communicating with the user.
print("Hello, world!") # Hello, world!
print(42) # 42
print(True) # TrueYou can print multiple values at once by separating them with commas. Python adds a space between them automatically:
name = "Ali"
age = 22
print(name, age) # Ali 22
print("Name:", name) # Name: Ali
print("Age:", age) # Age: 22Changing the separator
By default, print() puts a space between values. You can change this with the sep argument:
print("Ali", "Ahmed", "Khan") # Ali Ahmed Khan
print("Ali", "Ahmed", "Khan", sep="-") # Ali-Ahmed-Khan
print("Ali", "Ahmed", "Khan", sep="") # AliAhmedKhanChanging the end character
By default, print() adds a newline at the end — so each print starts on a new line. You can change this with the end argument:
print("Loading", end="")
print("...")
# Loading...
print("one", end=" ")
print("two", end=" ")
print("three")
# one two threeFormatting output
Printing raw variables works, but often you want to build a proper message. Python gives you a few ways to do this.
f-strings (recommended)
f-strings are the modern, cleanest way to format strings. Put an f before the opening quote, then wrap variable names in {}:
name = "Ali"
age = 22
print(f"My name is {name} and I am {age} years old.")
# My name is Ali and I am 22 years old.You can put any expression inside the curly braces — not just variable names:
a = 10
b = 3
print(f"{a} + {b} = {a + b}") # 10 + 3 = 13
print(f"{a} divided by {b} = {a / b:.2f}") # 10 divided by 3 = 3.33The :.2f part means "show this float with 2 decimal places". You will see more formatting options like this as you go.
.format() method
An older but still valid approach:
name = "Ali"
age = 22
print("My name is {} and I am {} years old.".format(name, age))
# My name is Ali and I am 22 years old.String concatenation
You can join strings together with +. But every value must be a string — numbers need to be converted first:
name = "Ali"
age = 22
print("My name is " + name) # My name is Ali
print("I am " + str(age) + " years old.") # I am 22 years old.Use f-strings. They are the cleanest, most readable option and the standard in modern Python. The other methods exist, but f-strings should be your default.
input() — getting input from the user
input() pauses the program and waits for the user to type something and press Enter. Whatever they type is returned as a string.
name = input("What is your name? ")
print(f"Hello, {name}!")When this runs:
What is your name? Ali
Hello, Ali!The string you pass to input() is the prompt — the message shown to the user before they type. It is optional, but always include it so the user knows what to enter.
input() always returns a string
This is the most important thing to understand about input(). No matter what the user types — a number, a decimal, anything — it comes back as a string.
age = input("Enter your age: ")
print(type(age)) # <class 'str'>If you want to do math with the input, you must convert it first:
age = input("Enter your age: ")
age = int(age)
print(f"In 10 years you will be {age + 10}.")Or on one line:
age = int(input("Enter your age: "))
print(f"In 10 years you will be {age + 10}.")If the user types something that cannot be converted — like typing "hello" when you call int() on it — Python raises a ValueError. You will learn how to handle this gracefully in the Error Handling section.
A simple example putting it together
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}!")
print(f"You were born around {2024 - age}.")Output:
Enter your name: Ali
Enter your age: 22
Hello, Ali!
You were born around 2002.Summary
| Function | Purpose | Returns |
|---|---|---|
print(value) | Display output | Nothing (None) |
input(prompt) | Get user input | Always a str |
f"text {var}" | Format a string | A formatted str |