Input and Output in Python

Python input and output are used to communicate between a Python program and the user. Output allows a program to display messages and results, while input allows the program to receive information from the user.

In this chapter, you will learn how to use Python's print() and input() functions, format output, work with f-strings and escape characters, and convert user input into different data types.

Input

print("Welcome to AHK Infotech")

Output

Welcome to AHK Infotech

1. print() Function in Python

The print() function is used to display text, numbers, variables, and results on the screen. It is one of the first Python functions beginners learn because it helps show what a program is doing.

Syntax

print(value)

Input

a = 10

b = 5

print(a)

print(a + b)

Output

Printing Multiple Values

You can provide multiple values to print().

Input:

name = "Amit"

age = 20

print("Name:", name)

print("Age:", age)

Output:

Name: Amit

Age: 20

10

15

2. input() Function in Python

The input() function is used to receive information from the user through the keyboard.

Syntax

variable = input("message")

Input

Explanation

name = input("Enter your name: ")

print("Hello", name)

Output

The program pauses at input() until the user enters a value and presses Enter.

If the user enters:

Amit

The output will be:

Hello Amit

3. Taking User Input

User input makes Python programs interactive. Instead of using fixed values, you can allow users to provide their own information.

Explanation

User Name and City

name = input("Enter your name: ")

city = input("Enter your city: ")

print("Name:", name)

print("City:", city)

Enter your name: Amit

Enter your city: Delhi

Name: Amit

City: Delhi

By default, the input() function returns the entered value as a string.

For example:

age = input("Enter your age: ")

Even if the user enters 20, Python initially treats age as a string.

This is important when you want to perform calculations using user input.

Output

Input

4. Formatting Output in Python

Output formatting allows you to present information in a clear and readable way.

Python provides several ways to format output, including commas in print(), string concatenation, and f-strings.

Using Commas

Input

name = "Amit"

age = 20

print("My name is", name, "and I am", age, "years old.")

Using sep (-)

The sep parameter controls the separator between multiple values.

My name is Amit and I am 20 years old.

Output

print("Hello", end=" ")

print("Python")

Output

Input

Using end

By default, print() moves to a new line after displaying a value. The end parameter can change this behavior.

Input

Output

name = "Amit"

city = "Delhi"

print(name, city, sep=" - ")

Amit - Delhi

Hello Python

5. f-Strings in Python

f-strings, also called formatted string literals, provide a simple way to insert variables and expressions directly into a string.

An f-string is created by placing f before the opening quotation mark.

Input

name = "Amit"

age = 20

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

The variables are placed inside curly braces {}.

f-String with an Expression

You can also place calculations inside an f-string.

f-strings are useful when you want to create clean and readable output.

My name is Amit and I am 20 years old.

Output

price = 500

quantity = 3

print(f"Total Price: ₹{price * quantity}")

Total Price: ₹1500

Output

Input

6. Escape Characters in Python

Escape characters are special sequences that are used inside strings to represent characters or formatting that would otherwise be difficult to write directly.

Escape sequences usually begin with a backslash \.

Common Escape Characters

Escape Character

Purpose

\n

New line

\t

Tab space

\"

Double quotation mark

\'

Single quotation mark

\\

Backslash

New Line (\n)

The \n escape sequence moves the following text to a new line.

Input

Output

print("Hello\nPython")

Hello

Python

Quotation Marks

Escape characters can be used when quotation marks need to appear inside a string.

Input

Output

print("Name:\tAmit")

print("Age:\t20")

Name: Amit

Age: 20

Tab (\t)

The \t escape sequence adds a tab space, which is often used in programming and text formatting to create a structured layout that improves readability and organization in the displayed content. By utilizing tab spaces effectively, developers and writers can create indents that visually separate different sections of information.

Input

Output

print("He said, \"Python is easy to learn.\"")

He said, "Python is easy to learn."

Backslash

Use \\ when you need to display a backslash. This character is crucial in programming and various types of documentation. It's often used in file paths and to escape characters in strings, so understanding its usage will enhance your coding skills significantly.

Input

Output

print("C:\\Python\\Projects")

C:\Python\Projects

7. Type Conversion with Input

Since input() returns a string, you often need to convert the entered value into another data type before performing calculations.

Python provides functions such as:

  • int() — converts a value to an integer

  • float() — converts a value to a floating-point number

  • str() — converts a value to a string

Converting Input to Integer

Use int() when the user needs to enter a whole number, which is essential for ensuring that calculations and data processing functions correctly. This function will cast input data into the Integer type, allowing it to be used in arithmetic operations and logic comparisons effectively.

Adding Two Numbers

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

total = num1 + num2

print("Total:", total)

Without int(), Python would treat the entered numbers as strings.

Input

Output

Enter first number: 25

Enter second number: 15

Total: 40

Converting Input to Float

Use float() when the user may enter decimal values, ensuring that your application can handle numerical inputs accurately, whether they are whole numbers or include fractions. This function is particularly useful in cases like calculating prices, measurements, or any scenario that requires precision.

price = float(input("Enter product price: ")) quantity = int(input("Enter quantity: "))

total = price * quantity

print(f"Total Price: ₹{total}")

Enter product price: 250

Enter quantity: 3

Total Price: ₹750.0

Output

Input

Practical Example: Student Information

Let's combine input, type conversion, and formatted output in one program to process and display student details interactively.

Input

Output

name = input("Enter your name: ")

age = int(input("Enter your age: "))

course = input("Enter your course: ")

print("\nStudent Information")

print("-------------------")

print(f"Name: {name}")

print(f"Age: {age}")

print(f"Course: {course}")

Enter your name: Amit

Enter your age: 20

Enter your course: Python

Student Information

-------------------

Name: Amit

Age: 20

Course: Python

This example demonstrates how Python can collect information from a user and display it in an organized format.

Contact

Reach out for AI courses and support

Email

Phone

careers@ahkacademy.in

Mo:- +91-7769920076

}

© 2025. All rights reserved.