Input & Output
Learn how to talk to your programs — print things to the screen and ask the user for information.
Every useful program needs two things: a way to show information to the user, and a way to receive information from the user. In Python, print() handles the first part and input() handles the second. Together they let you build programs that feel alive — programs that respond to the person using them.
See it in action
— step through the idea, then dive into the details below.Programs That Talk Back
Every useful program needs to show information and receive information. In Python, print() and input() are the two tools that make your programs feel alive.
#Printing to the Screen
You have already seen print() in action. At its simplest, you pass it a value and it displays that value on screen, then moves to the next line automatically.
print("Hello, world!")
print(42)
print(3.14)#Printing Multiple Values at Once
You can pass several values to print() separated by commas. Python puts a space between each one by default.
print("Your score:", 95, "out of", 100)#Customising the Separator
That automatic space is controlled by a special keyword argument called sep (short for separator). You can change it to anything — a dash, a comma, or even nothing at all.
print("2026", "07", "02", sep="-")
print("cat", "dog", "fish", sep=" | ")
print("a", "b", "c", sep="")#Controlling the Line Ending
By default print() adds a newline at the end, so the next print starts on a fresh line. You can change this with the end keyword argument. Setting end="" makes the next print continue on the same line.
print("Loading", end="")
print(".", end="")
print(".", end="")
print(".")Think of print() like a typewriter
A typewriter prints characters and then automatically moves to the next line when you hit Enter. end is like deciding whether to hit Enter or not. sep is like choosing what you type between words.
#Asking the User for Input
input() pauses your program, shows a message (called a prompt), and waits for the user to type something and press Enter. Whatever they type is handed back to you as a value you can store in a variable.
name = input("What is your name? ")
print("Nice to meet you,", name)input() ALWAYS gives you a string
Even if the user types 42, Python gives you the text "42", not the number 42. If you try to do maths with it without converting, you will get a surprising result.
``python age = input("How old are you? ") # user types 20 print(age + 1) # ERROR: can't add string and number ``
Always convert with int() or float() when you need a number.
#Converting Input to a Number
Wrap input() in int() to get a whole number, or float() to get a decimal number. This is one of the most common patterns you will write.
age = int(input("How old are you? "))
next_year = age + 1
print("Next year you will be", next_year)#F-strings: Clean, Readable Output
An f-string lets you embed variables directly inside a string. Put an f before the opening quote and wrap your variable names in {}. Python replaces each {} with the variable's value. This is usually much neater than using commas in print().
name = "Maria"
age = 28
print(f"Hello, {name}! You are {age} years old.")You can even put simple expressions inside the {}.
price = 4.5
quantity = 3
print(f"Total: ${price * quantity:.2f}")#Putting It All Together
Here is a small interactive script that uses everything from this lesson: input(), type conversion, and an f-string for friendly output.
name = input("What is your name? ")
year_born = int(input("What year were you born? "))
current_year = 2026
age = current_year - year_born
print(f"Hi {name}! You are around {age} years old.")Leave a space inside the prompt string
Write input("Your name: ") with a trailing space inside the quotes. Without it, the cursor appears right after the colon — Your name:_ — which looks cramped and is harder to read.
A user types the number 5 when your program runs `score = input("Score: ")`. What is the value of `score` afterwards?
Key takeaways
- print() can take multiple values separated by commas, with sep controlling what goes between them and end controlling the line ending.
- input(prompt) pauses the program, shows a prompt, and returns whatever the user typed as a string.
- Always convert input to int() or float() before doing any arithmetic with it.
- F-strings (f"Hello, {name}") are the cleanest way to embed variables inside text.
- A well-placed space at the end of your input prompt makes your program look polished.
What does this code print?
print("a", "b", "c", sep="-")
print("done", end="!")
print("!")This code is supposed to print the user's age next year, but it crashes. What is wrong?
age = input("How old are you? ")
next_year_age = age + 1
print(f"Next year you will be {next_year_age}")Complete the f-string so the code prints: Hello, Jordan! You are 17 years old.
name = "Jordan" age = 17 print(f"Hello, ! You are years old.")
Put these lines in the right order to ask for a price, apply a 10% discount, and print the result.
discount = price * 0.10
print(f"You save ${discount:.2f}, final price: ${final:.2f}")final = price - discount
price = float(input("Enter price: "))Build a simple tip calculator. Ask the user for the meal cost (as a decimal number) and the tip percentage they want to leave (as a whole number). Then print the tip amount and the total to pay, both rounded to 2 decimal places. Use an f-string for the output.
Try it live — edit the code and hit Run to execute real Python: