Python BasicsBeginner8 min12 / 63

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

Visual walkthrough1 / 6
1

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.

Think of print() as your program speaking and input() as your program listening.

#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() works with text, whole numbers, and decimals
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.

Commas separate the values; spaces appear automatically
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.

sep lets you choose what goes between each value
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.

end="" removes the automatic newline
print("Loading", end="")
print(".", end="")
print(".", end="")
print(".")
Tip

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.

input() always returns what the user typed
name = input("What is your name? ")
print("Nice to meet you,", name)
Common mistake

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.

int() converts the string "20" into the number 20
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().

f-strings keep your output readable at a glance
name = "Maria"
age = 28
print(f"Hello, {name}! You are {age} years old.")

You can even put simple expressions inside the {}.

:.2f rounds the number to 2 decimal places inside the f-string
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.

A complete interactive greeting program
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.")
Watch out

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.

Quick check

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.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
print("a", "b", "c", sep="-")
print("done", end="!")
print("!")
Fix the bug#2

This code is supposed to print the user's age next year, but it crashes. What is wrong?

fix-bug
age = input("How old are you? ")
next_year_age = age + 1
print(f"Next year you will be {next_year_age}")
Fill in the blank#3

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.")
Reorder the lines#4

Put these lines in the right order to ask for a price, apply a 10% discount, and print the result.

1
discount = price * 0.10
2
print(f"You save ${discount:.2f}, final price: ${final:.2f}")
3
final = price - discount
4
price = float(input("Enter price: "))
Your turn
Practice exercise

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:

solution.py · editable