DEV Community

Shahrouz Nikseresht
Shahrouz Nikseresht

Posted on

Python Input and Output Explained Simply

Input and output operations let your programs interact with users. Python makes this straightforward with input() and print().

Getting input from the user

Use input() to read text from the keyboard. It always returns a string.

name = input("Enter your name: ")
print(name)
Enter fullscreen mode Exit fullscreen mode

Add a prompt inside input():

age = input("How old are you? ")
Enter fullscreen mode Exit fullscreen mode

The program waits for the user to type something and press Enter.

Converting input types

Since input() returns a string, convert it when needed.

age = int(input("How old are you? "))
print(age + 1)  # Adds 1 to the number
Enter fullscreen mode Exit fullscreen mode

Common conversions:

  • int() for whole numbers
  • float() for decimals
  • str() to convert back to text

Without conversion:

number = input("Enter a number: ")  # "10"
result = number + "5"               # "105" (concatenation, not addition)
Enter fullscreen mode Exit fullscreen mode

Basic output with print

Use print() to display values.

print("Hello, world!")
print(42)
print(True)
Enter fullscreen mode Exit fullscreen mode

Print multiple items:

name = "Alex"
score = 95

print("Name:", name, "Score:", score)
Enter fullscreen mode Exit fullscreen mode

Use f-strings for cleaner output:

print(f"{name} scored {score} points.")
Enter fullscreen mode Exit fullscreen mode

Control formatting:

pi = 3.14159
print(f"Pi rounded: {pi:.2f}")  # Pi rounded: 3.14
Enter fullscreen mode Exit fullscreen mode

Simple program example

A basic calculator addition:

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

sum_result = num1 + num2
print(f"The sum is: {sum_result}")
Enter fullscreen mode Exit fullscreen mode

Common mistake: forgetting type conversion

This causes errors when doing math:

age = input("Age: ")  # "25"
next_year = age + 1    # TypeError
Enter fullscreen mode Exit fullscreen mode

Fix:

next_year = int(age) + 1
Enter fullscreen mode Exit fullscreen mode

Quick summary

  • input() reads user text (always as string).
  • Convert with int(), float(), etc. for calculations.
  • print() displays output; use f-strings for formatting.
  • Combine them to make interactive programs.

Practice building small interactive scripts. Input and output are key for creating useful Python programs.

Top comments (0)