Python Tutorials | Understanding Input and Output in Python

When building any kind of application, interaction with the user is important. Python provides two simple built-in functions to handle this: input() for taking user input, and print() for displaying output.

1. The print() Function

The print() function is used to send text or data to the screen. It can print strings, numbers, variables, or even formatted text.

2. The input() Function

The input() function waits for the user to type something and press Enter. Whatever is typed is returned as a string.

Example 1: Basic Output with print()

print("Welcome to Python programming!")

This example displays a simple message to the user.

Example 2: Basic Input with input()

name = input("Enter your name: ")
print("Hello,", name)

This script takes user input and greets the person using the name entered.

Example 3: Input with Type Conversion

age = int(input("Enter your age: "))
print("You will be", age + 1, "next year.")

The input() function always returns a string. To perform numeric operations, you need to convert the input using int(), float(), etc.

Example 4: Formatted Output

name = "Alice"
score = 95
print(f"{name} scored {score} out of 100.")

This example uses an f-string to format the output in a readable way. This is available in Python 3.6 and above.

Example 5: Multiple Inputs and Outputs

first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
print("Full Name:", first_name + " " + last_name)

This script takes two inputs and prints the combined full name using string concatenation.


Summary

  • print() is used to display messages and results.
  • input() is used to collect data from the user as a string.
  • Convert user input to other types when needed using int(), float(), etc.
  • Use formatted strings to produce clean and dynamic output.

Understanding how to handle input and output in Python is essential for building interactive scripts, utilities, and applications.