Input and Output in Python

Input and Output in Python

Understanding input and output operations is fundamental to Python programming. The print() function displays output in various formats, while the input() function enables interaction with users by gathering input during program execution.

Printing Output using print() in Python

At its core, printing output in Python is straightforward, thanks to the print() function. This function allows us to display text, variables, and expressions on the console.

Basic Usage of print()

Let’s begin with a simple example:

print("Hello, World!")

Output: Hello, World!

Printing Variables

You can use the print() function to print single and multiple variables. Here’s how:

# Single variable
username = "Charlie"
print(username)

# Multiple Variables
username = "Evelyn"
age = 30
city = "San Francisco"
print(username, age, city)

Output:

Charlie

Evelyn 30 San Francisco

Output Formatting

Output formatting in Python can be achieved using various techniques, including:

  • format() method
  • Manipulation of the sep and end parameters
  • F-strings
  • Percentage (%) operator

Example 1: Using format()

amount = 250.50
print("Amount: ${:.2f}".format(amount))

Output: Amount: $250.50

Example 2: Using sep and end parameters

# end Parameter with '@'
print("Python", end='@')
print("Programming")

# Separating with Comma
print('A', 'B', 'C', sep='')

# Formatting a date
print('10', '11', '2025', sep='-')

# Another example
print('john', 'doe', sep='@')

Output:

Python@Programming

ABC

10-11-2025

john@doe

Example 3: Using F-strings

name = 'Alex'
age = 25
print(f"Hello, My name is {name} and I'm {age} years old.")

Output: Hello, My name is Alex and I'm 25 years old.

Example 4: Using Percentage (%) Operator

# Taking input from the user
num = int(input("Enter a number: "))
add = num + 10
print("The sum is %d" %add)

Output:

Enter a number: 40 The sum is 50

Taking Multiple Inputs in Python

You can take multiple inputs from the user in a single line and split them into separate variables using the split() method. Here’s an example:

# Taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of apples: ", x)
print("Number of oranges: ", y)

# Taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of fruits: ", x)
print("Number of apples is: ", y)
print("Number of oranges is: ", z)

Output:

Enter two values: 5 8

Number of apples: 5

Number of oranges: 8

Enter three values: 4 7 10

Total number of fruits: 4

Number of apples is: 7

Number of oranges is: 10

Conditional Input from User

In this example, the program prompts the user to enter their age. The input is converted to an integer, and conditional statements determine the appropriate response based on the age entered:

# Prompting the user for input
age_input = input("Enter your age: ")
age = int(age_input)
if age < 0:
    print("Please enter a valid age.")
elif age < 18:
    print("You are a minor.")
elif age >= 18 and age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

Output:

Enter your age: 30

You are an adult.

Input and Output in Python

The input() function is used to take user input, returning the input as a string by default:

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

Output:

Enter your name: Alice

Hello, Alice! Welcome!

Changing the Type of Input in Python

To take input as an integer or float, you can typecast it using the int() or float() functions.

Example: Taking Input as an Integer

# Taking input as int
n = int(input("How many flowers?: "))
print(n)

Output:

How many flowers?: 12

12

Example: Taking Input as a Float

# Taking input as float
price = float(input("Price of each flower?: "))
print(price)

Output:

Price of each flower?: 15.75

15.75

Find DataType of Input in Python

To determine the type of a variable in Python, you can use the type() function:

a = "Hello World"
b = 20
c = 22.50
d = ("Python", "is", "fun")
e = ["Python", "rocks"]
f = {"Python": 1, "Java": 2}
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))

Output:

<class 'str'>

<class 'int'>

<class 'float'>

<class 'tuple'>

<class 'list'>

<class 'dict'>

Input and Output in Python – FAQs

What are Input and Output Files in Python?

In Python, input files are files from which a program reads data. These files can be text files, binary files, or any other data formats that the program needs to ingest and process. Output files are files to which a program writes data, such as results, logs, or any processed data for later use.

What is the Difference Between Input and Output?

Input refers to data or signals received by a system or process, while output is the result of processing that input. In Python programming, input can be user commands or data from files, and output can be printed to the console or written to files.

What is Input Process Output in Python?

The Input, Process, Output (IPO) model in Python refers to gathering data (input), performing computations (process), and displaying or storing results (output), structuring programs in a logical manner.

How to Write Output in Python?

Output can be written in several ways:

  • Printing to the Console: Use the print() function.
  • Writing to a File: Use Python’s file handling capabilities.
  • Using Standard Output Streams: Utilize sys.stdout for more control.

© 2025 SeekCircuit. All rights reserved.