PythonPython FundamentalsHandling Input and Output

Handling Input and Output in Python

Python provides simple and powerful ways to display output and take user input. Let’s explore the basics of print() and input() functions.


1. Using print() for Output

The print() function is used to display messages, variables, or results of expressions to the console.

Basic Usage:

print("Hello, Pythonforall!")

Output:

Hello, Pythonforall!

Parameters of print()

The print() function offers several optional parameters for formatting:

  • sep: Specifies the separator between multiple values. Default is a space.
  • end: Specifies what to print at the end. Default is a newline (\n).
  • file: Sends the output to a file instead of the console.

Example with Parameters:

# Using 'sep' to change the separator
print("Python", "is", "fun", sep="-")  # Output: Python-is-fun
 
# Using 'end' to avoid a newline
print("Python", end="!")               # Output: Python!
 
# Combining 'sep' and 'end'
print("Learn", "Python", sep="@", end=" :)")  # Output: Learn@Python :)

2. Using print(f{}) for Formatted Output

In Python, f-strings provide an easy way to format strings by embedding expressions inside curly braces {}.

Example:

name = "Aman"
age = 25
 
print(f"My name is {name} and I am {age} years old.")

Output:

My name is Aman and I am 25 years old.

Why Use f-strings?

  • Cleaner and more readable than concatenation (+).
  • Supports inline calculations and function calls:
    print(f"5 + 3 = {5 + 3}")

3. Taking User Input with input()

The input() function allows the program to take input from the user. The input is always returned as a string.

Basic Usage:

name = input("Enter your name: ")
print(f"Hello, {name}!")

Example of Taking Numeric Input:

To handle numbers, you need to convert the input string into the desired numeric type:

age = int(input("Enter your age: "))  # Convert input to an integer
height = float(input("Enter your height: "))  # Convert input to a float
 
print(f"Your age is {age} and your height is {height} meters.")

Combining input() and print() for Interactive Programs

Here’s a program that uses both input() and print() to create a simple user interaction:

Example:

# Taking user details
name = input("What is your name? ")
age = int(input("How old are you? "))
 
# Printing a personalized message
print(f"Welcome, {name}! You are {age} years old.")

Summary of Functions

FunctionPurposeExample
print()Display output to the console.print("Hello!")
print(f{})Format and display variables inline.print(f"My name is {name}")
input()Take input from the user.name = input("Enter your name: ")
int(input())Take numeric input (integer).age = int(input("Enter your age: "))
float(input())Take numeric input (float).height = float(input("Enter height: "))

Try It Yourself

Problem 1: Write a program to take your friend’s name and age as input, then display a greeting message like:

Hello, [Name]! You are [Age] years old.
Show Code
name = input("Enter your friend's name: ")
age = int(input("Enter their age: "))
 
print(f"Hello, {name}! You are {age} years old.")

Problem 2: Write a program to take two numbers as input and display their sum, difference, product, and division.

Show Code
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
 
print(f"The sum is: {num1 + num2}")
print(f"The difference is: {num1 - num2}")
print(f"The product is: {num1 * num2}")
print(f"The division is: {num1 / num2}")
<br />

<CodeRunner />