Python Input and Output
Learn Python input and output: print() with sep and end, reading text with input(), converting strings to numbers with int() and float(), and f-strings. Runnable code, a worked example and a quiz.
Key takeaways
- print() sends output to the screen; you can pass several values and control sep and end
- input() pauses the program and reads a line of text the user types
- input() always returns a string, so convert with int() or float() to do maths
- f-strings let you drop variables straight into text with {name} for clear output
Talking to the computer, both ways
Every useful program does two things: it shows information to a person, and it takes in information from a person. In Python, those two jobs belong to print() and input(). Together they are called output (what the program shows) and input (what the user types). Master these two functions and your programs stop being silent calculations — they start having conversations.
This lesson assumes you can run a Python program. If you have not yet, work through your first Python program first, then come back.
Output: the print() function
You have probably already met print(). It sends text to the screen:
print("Hello, world!")
print("Learning Python is fun.")
This produces two lines:
Hello, world!
Learning Python is fun.
But print() can do more than show one piece of text. You can pass it several values separated by commas, and it prints them all with a space between:
name = "Maya"
age = 12
print("Name:", name, "Age:", age)
Output:
Name: Maya Age: 12
Notice Python automatically put spaces between the items and turned the number 12 into text for you.
Controlling print with sep and end
Two special settings change how print() behaves: sep and end.
sep (separator) sets what goes between the values:
print("2024", "11", "30", sep="-") # 2024-11-30
print("a", "b", "c", sep="") # abc
end sets what is printed after everything, instead of starting a new line. By default it is a newline, which is why each print() lands on its own line. Change it to keep printing on the same line:
print("Loading", end="")
print("...", end="")
print("done!")
Output:
Loading...done!
All three prints share one line because the first two used end="" instead of a newline.
Input: the input() function
input() pauses your program, waits for the user to type a line and press Enter, then hands that text back to you:
name = input("What is your name? ")
print("Nice to meet you,", name)
When you run it, the program prints the prompt, stops, and blinks a cursor. If you type Sam and press Enter, you see:
What is your name? Sam
Nice to meet you, Sam
The text inside input("...") is the prompt — a message telling the user what to type. Always include a clear prompt so the user knows what is expected.
The golden rule: input() always returns a string
Here is the single most important fact in this whole lesson, and the source of countless beginner bugs:
input() always gives you back a string — text — even if the user types a number.
Watch what goes wrong:
age = input("How old are you? ")
print(age + 1) # ERROR!
If you type 12, Python stores the text "12", not the number 12. Trying to add the number 1 to text causes a TypeError. You cannot add a number to a string.
Converting text to numbers with int() and float()
To do maths, you must convert the text into a number first:
int(...)turns text into a whole number (integer).float(...)turns text into a decimal number.
age = int(input("How old are you? "))
print("Next year you will be", age + 1)
price = float(input("Enter a price: "))
print("With tax:", price * 1.2)
Now int(input(...)) reads the text and immediately converts it to a number, so the maths works. Use int() for whole numbers like age or count, and float() for decimals like price or weight.
One warning: int("hello") or int("3.5") will crash with a ValueError, because that text is not a whole number. For now, assume the user types sensibly; handling bad input neatly comes later.
f-strings: the clean way to build output
Joining text and variables with commas works, but f-strings are clearer and more powerful. An f-string starts with the letter f before the quote, and you drop variables straight into the text using curly braces {}:
name = "Maya"
age = 12
print(f"{name} is {age} years old.")
Output:
Maya is 12 years old.
The {name} and {age} are replaced by the variables' values. You can even put calculations inside the braces:
price = 8
print(f"Three items cost {price * 3} pounds.") # Three items cost 24 pounds.
f-strings are the standard, readable way to build output in modern Python. To go deeper into shaping text, see working with text and strings.
Worked example: a simple greeting and age calculator
Let's combine everything into one small, complete program that talks to the user.
print("Welcome to the Age Machine!")
name = input("What is your name? ")
birth_year = int(input("What year were you born? "))
current_year = 2025
age = current_year - birth_year
print(f"Hello, {name}!")
print(f"In {current_year} you turn {age} years old.")
print(f"In ten years you will be {age + 10}.")
Step by step, here is what happens:
print("Welcome to the Age Machine!")shows a heading — pure output.input("What is your name? ")pauses and reads the name as text. A name stays as a string, so no conversion is needed.int(input("What year were you born? "))reads the birth year and converts it to a whole number, because we will do maths with it.current_year - birth_yearcalculates the age using two numbers.- The three f-string prints build clear sentences, dropping
name,ageand even the calculationage + 10straight into the text.
If a user enters Maya and 2013, the output is:
Welcome to the Age Machine!
What is your name? Maya
What year were you born? 2013
Hello, Maya!
In 2025 you turn 12 years old.
In ten years you will be 22.
Try it yourself
Build a small program that has a real conversation with the user:
- Ask for the user's name and store it as text.
- Ask for the number of pets they have, converting it with
int(). - Ask for the price of one item, converting it with
float(). - Print a friendly summary using f-strings, for example:
Maya has 2 pets, and 3 items cost 24.0 pounds. - Experiment with
sepandendon at least oneprint()to control its layout.
When your program reads input cleanly and prints clear output, you are ready to make it react to different answers. That is the job of conditionals — see conditionals in Python next.
Quick quiz
Test yourself and earn XP
What type of value does input() always return?
input() always returns a string. Even if the user types 42, you get the text "42" and must convert it with int() to do maths.
What does this print? ``` print("A", "B", "C", sep="-") ```
The sep argument sets what goes between values. With sep="-", the items are joined by dashes: A-B-C.
Why does `int(input("Age: ")) + 1` work, but `input("Age: ") + 1` cause an error?
input() gives back text. Adding the number 1 to text is a TypeError. Wrapping it in int() turns the text into a number first.
What does the end parameter control in print()?
By default print() ends with a newline. Setting end="" or end=" " changes what comes after, so the next print can stay on the same line.
Which f-string correctly inserts the variable name into the text?
An f-string starts with f and uses curly braces around the variable: f"Hello {name}" inserts the value of name.
FAQ
int() can only convert text that looks like a whole number. If the user types letters, a decimal point, or leaves it blank, int() raises a ValueError. For decimals use float() instead, and for safety you can check or handle invalid input before converting.
print() displays text on the screen for a person to read, but does not give a value back to your program. return (used inside functions) hands a value back so the rest of your code can use it. This lesson is about print and input; return is covered with functions.
Keep exploring
More in Coding