Input and Output in Python | Python Course | Dataplexa

Input and Output in Python

Every useful program does two things — it receives information and it shows results. In Python, these two actions are handled by just two functions: print() for output and input() for input. They are among the first things you will use in almost every program you write.

In this lesson you will learn how to display text and values on the screen, how to control the way output looks, and how to ask a user for information and use it in your program.

Displaying Output with print()

The print() function shows information on the screen. You can pass it text, numbers, variables, or even the result of a calculation — and it will display whatever you give it.

print("Welcome to Dataplexa")
print(100)
print(10 + 5)
Welcome to Dataplexa 100 15
  • print("text") displays a string — text must be inside quotes.
  • print(100) displays a number — numbers do not need quotes.
  • print(10 + 5) displays the result of the calculation, which is 15.
  • Each print() call starts on a new line by default.

Printing Variables and Labels Together

A very common pattern is to print a label alongside a variable's value. This makes output easy to read and understand.

name = "Dataplexa"
year = 2026

print("Platform:", name)
print("Year:", year)
Platform: Dataplexa Year: 2026
  • You can pass multiple items to print() separated by commas.
  • Python automatically adds a single space between each item.
  • This is one of the most useful patterns for showing results clearly.

Controlling How Output Looks

The print() function has two optional settings that give you more control over how output is displayed.

  • sep — changes what goes between printed items (default is a space).
  • end — changes what is added at the very end (default is a new line).
print("A", "B", "C", sep=" - ")
print("Loading", end="...")
print("Done")
A - B - C Loading...Done
  • sep=" - " puts a dash between A, B, and C instead of a space.
  • end="..." stops a new line from being added, so the next print continues on the same line.
  • These settings are useful when you want to format output in a specific way.

Formatted Strings with f-strings

An f-string is a cleaner way to mix variables and text in a single print statement. You add the letter f before the opening quote, then put variable names inside curly braces {}.

name = "Alex"
score = 95

print(f"Hello, {name}! Your score is {score}.")
Hello, Alex! Your score is 95.
  • The f before the quote makes it a formatted string.
  • Anything inside {} is replaced with the actual value of that variable.
  • F-strings are the most modern and readable way to build output strings in Python.

Receiving Input with input()

The input() function pauses the program and waits for the user to type something. Whatever the user types is saved into a variable for use later in the program.

user_name = input("Enter your name: ")
print("Hello,", user_name)
Enter your name: Alex Hello, Alex
  • The text inside input("...") is shown to the user as a prompt.
  • The user types their response and presses Enter.
  • Whatever they typed is stored in the variable on the left.

Input Always Returns Text

One very important thing to know about input() — it always returns a string, even if the user types a number. This means if you want to do math with the input, you must first convert it to a number.

age_text = input("Enter your age: ")
age = int(age_text)   # convert the string to a whole number

print("Next year, your age will be:", age + 1)
Enter your age: 24 Next year, your age will be: 25
  • int() converts text like "24" into the actual number 24.
  • Without this conversion, age + 1 would cause an error because you cannot add a number to text.
  • You can also write this in one line: age = int(input("Enter your age: "))

Converting Input to Decimal Numbers

When the input could be a decimal — like a price or a measurement — use float() instead of int() to convert it.

height = float(input("Enter your height in meters: "))
print(f"Your height is {height} meters.")
Enter your height in meters: 1.75 Your height is 1.75 meters.
  • float() handles decimal numbers like 1.75 or 49.99.
  • Use int() for whole numbers and float() for anything with a decimal point.

Real World Example — A Simple Bill Calculator

This program asks the user for a price and a quantity, then calculates and displays the total. It uses input, conversion, calculation, and formatted output all together.

price = float(input("Enter the price per item: "))
qty = int(input("Enter the quantity: "))

subtotal = price * qty

print(f"Price per item: {price}")
print(f"Quantity: {qty}")
print(f"Total: {subtotal}")
Enter the price per item: 49.99 Enter the quantity: 2 Price per item: 49.99 Quantity: 2 Total: 99.98
  • float() is used for the price because it has decimal places.
  • int() is used for quantity because you cannot buy half an item.
  • The f-string makes the output clean and easy to read without extra commas or concatenation.

Common Mistakes with Input and Output

These are the mistakes beginners make most often when working with print and input:

  • Forgetting to convert input — If you type a number but do not convert it, Python treats it as text and math operations will fail.
  • Missing quotes around textprint(Hello) will cause an error. Text must be in quotes: print("Hello").
  • Confusing the prompt with the value — The text inside input("...") is just a message shown to the user. The actual value typed by the user is what gets stored.
  • Printing a variable before creating it — You must assign a value to a variable before you can print it.

Practice

Which function is used to display output on the screen?



Which function is used to receive text typed by a user?



What type of value does input() always return?



Which function converts text into a whole number?



Which function converts text into a decimal number?



Quick Quiz

Which print() parameter controls what goes between printed items?





Which print() parameter controls what is added at the end of output?





A user types 10 and you want to add 5 to it. Which conversion do you need?





NEXT UP
Operators in Python
Learn how to perform calculations, compare values, and combine conditions using Python's arithmetic, comparison, and logical operators.