Skip to main content

Python input and output (I/O) Statements Explained with Examples

Python Input and Output I/O with simple examples

Introduction

Python Input and Output are the foundation of interactive programming. Input allows users to enter information, while output displays results on the screen. By learning these concepts, you can create programs that communicate with users, solve real-world problems, and build applications that respond to user input effectively.

Hinglish:Python Input aur Output interactive programming ki foundation hai. Input ka use karke user se information li jaati hai, aur Output ka use karke screen par result dikhaya jaata hai. In concepts ko seekhne ke baad aap aise programs bana sakte ho jo users se communicate karein, real-world problems solve karein, aur user ke input ke hisaab se response dene wale applications develop karein.

Learning Objectives

After completing this lesson, you will be able to:
1.Understand the concept of Input and Output in Python.
2.Use the input() function to take data from users.
3.Use the print() function to display information.
4.Convert user input into different data types using Type Casting.
5.Format output using f-strings, sep, and end.
6.Write interactive Python programs using input and output.
7.Avoid common beginner mistakes related to user input.
8. Build simple real-world programs with confidence.

Table of Content

  • 1. What is Input and Output in Python?

  • 2. Python print() Function

  • 3. Python input() Function

  • 4. How input() Works in Python

  • 5. Taking Integer Input (int())

  • 6. Taking Float Input (float())

  • 7. Taking Multiple Inputs

  • 8. Output Formatting with f-Strings

  • 9. print() Parameters (sep and end)

  • 10. Escape Characters (\n, \t, \\)

  • 11. Common Input and Output Errors

  • 12. Practice Questions

  • 13. Mini Project

  • 14. Summary

1. What is Input and Output in Python?

Input is used to receive data from the user, and Output is used to display data or results on the screen. This process is called Input and Output in Python. It helps the Python program communicate with the user.
For example, a program asks the user to enter their name and displays a message like "Hello, Hamid!" or "Good Morning, Hamid!".

Example

Python

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

Output

Python

Enter your name:Hamid
Hello,Hamid
Goodmorning,Hamid

Explanation
  • The program starts by displaying "Enter your name:" using the input() function.
  • The program waits until the user types a name and presses the Enter key.
  • The entered name is stored in the name variable.
  • The print() function then displays the output on the screen.
  • If the user enters Hamid, the output will be Hello, Hamid and Good Morning, Hamid.
  • This is how Input and Output work in Python.

2.Python print() Function

What is the print() Function?

The print() function is a built-in (predefined) function in Python. This means it is already created by Python, so you do not need to define it yourself. You only need to use it in your program.

The print() function is used to display information or output on the screen. It helps users see messages, results, and other information generated by the program. You can use it to display text, numbers, symbols, variables, and program results.

Real-Life Example

When you open an app or website like Instagram or ChatGPT, the first screen usually shows options such as Sign Up or Log In. You may also see instructions like:

  • Enter your Name
  • Enter your Email
  • Enter your Password
  • Click the Login button

After you submit your information, the screen displays a message such as "Login Successful", "Invalid Password", or "Welcome!".

In programming, the print() function is used to display similar messages, instructions, and results on the screen so users can understand what the program is doing.

3..Python input() Function

The input() function is a built-in Python function used to take user input and store the entered data in a variable.

Example

Python

age = input("Enter your age: ")
print("Your age is", age)

Output

Python

Enter your age:19
Your age is 19

Explanation

The program asks the user to enter a name using input(). The entered name is stored in the name variable, and print() displays a personalized greeting message.

4.How input() Works in Python

  1. The input() function asks the user to enter data and stores the entered value in a variable.
  2. The message inside input() is displayed on the screen, and the program waits until the user enters data and presses the Enter key.
  3. The print() function is used to display the stored data or result on the screen.

5.Taking Integer Input (int())

  • int(input()) is used to take whole number input from the user.
  • Whole numbers are like 1, 5, 10, 100 (no decimal values).
  • By default, input() stores data as a string.
  • int() converts the entered value into an integer.
  • It is used to perform mathematical operations like addition, subtraction, multiplication, and division.

Example

Python

a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("The sum of two numbers is:", a + b)

Output

Python

Enter the first number:10
Enter the second number:5
The sum of two numbers is:15

6.Taking Float Input (float())

  • float(input()) is used to take decimal number input from the user.
  • Decimal numbers are like 2.5, 10.75, 99.99.
  • By default, input() returns data as a string.
  • float() converts the entered value into a float.
  • It is used for calculations with decimal numbers.

Example

Python

price = float(input("Enter the price of Product: "))
print("Price of Product is:", price)

Output

Python

Enter the price Product: 99.99
Price of Product is: 99.99

7.Take Multiple Values in Python

Definition

The input() function can take two or more values from the user in a single line. This is called Multiple Input.

Key Points

  • One input() function is used to take multiple values.
  • name and age are two different variables.
  • .split() breaks one string into multiple parts.
  • By default, .split() separates values using a space.
  • The first value is stored in the first variable, and the second value is stored in the second variable.
  • By default, input() returns all values as strings.
  • To perform mathematical operations, use int() or float() with map().

Example 1: Multiple String Input

Python

name, age = input("Enter your name and age: ").split()
print(name, age)
# or
print(name)
print(age)

Output

Python

Enter your name and age: Hamid 19
Hamid 19
Hamid
19

🍕 Real-Life Example: How .split() Works

Imagine you have one pizza 🍕 and two people 👨👩 want to eat it. The pizza is cut into multiple slices 🔪🍕 so everyone gets a separate piece.

In the same way, .split() breaks one string into multiple parts.

User enters:

Hamid 19

After using .split():

"Hamid 19"
⬇️
["Hamid", "19"]

Python stores the values like this:

Python

name = "Hamid"
age = "19"

Remember:

  • 🍕 One Pizza → 🍕🍕 Multiple Slices
  • 📝 One String → ✂️ Multiple Values using .split()

Example 2: Multiple Integer Input

Python

a, b = map(int, input("Enter two numbers: ").split())
print("Sum:", a + b)

Output

Python

Enter two numbers: 10 20
Sum: 30

How map() Works

  • .split() separates the entered values.
  • map() applies the same function to every value.
  • int converts every value into an integer.
  • float converts every value into a decimal number.
  • After conversion, mathematical operations can be performed.

Important Note

The number of variables and the number of values entered by the user must be the same. Otherwise, Python will show an error.

✅ Correct:

Python

name, age = input().split()
Input:
Hamid 19

❌ Incorrect:

Python

name, age = input().split()
Input:
Hamid

This gives an error because Python expects two values, but the user enters only one value.

8.Output Formatting with f-Strings

What is an f-String?

An f-string is a simple and modern way to display variables inside a string. It makes the output more readable and easier to write.

Key Points

  • Used to display variables inside a string.
  • Start the string with f.
  • Write variables inside { } (curly braces).
  • Makes the code clean and easy to read.
  • Commonly used with the print() function.

Syntax

Python

  print(f"Text {variable}")

Example

Python

name,age=input("Enter name and age:").split()
pritn(f"The Name is {name} and Age is {age} Years old")

Output

Python

Enter name and age:Raju 20
The Name is Raju and Age is 20 years old

9.print() Parameters (sep and end)

What are sep and end?

sep and end are optional parameters of the print() function. They are used to format the output and make it more readable.

sep Parameter

The sep (separator) parameter is used to separate multiple values printed by the print() function.

By default, Python uses a space between values. You can change it to any symbol like -, /, |, ,, etc.

Example

Python

print("Date", "Month", "Year", sep="/")

Output

Python

Date/Month/Year

Explanation

  • sep="/" adds / between each value.
  • You can use any separator such as -, |, or *.

Example

Python

print("Python", "Java", "C++", sep=" | ")

Output

Python

Python | Java | C++

end Parameter

The end parameter is used to change what is printed after the output.

By default, every print() statement starts on a new line. Using end allows the next print() statement to continue on the same line.

Example

Python

print("Hamid", end=" ")
print("Hasnain", end=" ")
print("Ali", end=" ")
print("Raju")

Output

Python

Hamid Hasnain Ali Raju

Explanation

  • By default, print() moves to the next line.
  • end=" " adds a space instead of a new line.
  • All values are printed on the same line.

📝 Remember

  • sep = Separates multiple values.
  • end = Decides what comes after the output.

10. Escape Characters (\n, \t, \\)

An escape character is a special character used inside a string to represent special characters or perform special actions.
In Python, the escape character is the backslash (\).

There are many escape sequences, but some common and important ones are:

  • \n → New line
  • t → Tab space
  • \\ → Backslash
  • \' → Single quote
  • \" → Double quote

These escape sequences are commonly used by programmers when working with strings.

Example of New Line \n Escape Character

\n is used to create a new line. It allows you to print multiple lines using a single print() statement.
\n is written inside the string.

Python

print("Hello, How are you?\nWhat is your name?\nWhat is your work?")

Output

Python

Hello, How are you?
What is your name?
What is your work?

Example of Tab space \t Escape Character

\t is used to add a tab space between two or more strings or pieces of text.
\t is written inside the string.

Python

print("Name:\tHamid")
print("Age:\t19")

Output

Python

Name: Hamid 
Age: 20

Example of Backslash \\ Escape Character

\\ is used to print a backslash (\) inside a string.
Example Print a date from users

Python

day=int(input("Enter Day:")
month=int(input("Enter Month:")
years=int(input("Enter Years:")
print(f"Current Date:\t{day}\\{month\\{years}")

Explanation

  • input() → Takes Day, Month, and Year from the user.
  • int() → Converts the input into an integer.
  • f" " → Allows variables to be used directly inside the string.
  • \t → Adds a tab space.
  • \\ → Prints a backslash (`\`).
  • {day}, {month}, {years} → Display the values entered by the user.

Output

Python

Enter Day: 4
Enter Month:8
Enter Years:2026
Current Date: 4\8\2026

Example of Single Quote \' Escape Character

\' is used to print a single quote (') inside a string.

Python

print('It\'s Python')

Output

Python

It's Python

Example of Double Quote \" Escape Character

\" is used to print a double quote (") inside a string.

Python

print("He said \"Hello\"")

Output

Python

He said "Hello"

Comments

Popular posts from this blog

Python Variables and Data Types Explained with Examples

🚀 Welcome to NiceinPythonlearn | 📘 Learn Python with Easy Tutorials, Notes & Practice Questions | 💻 Build Real-World Projects | 💬 Enjoying the content?Share your feedback! | 📝 If you have any questions, suggestions, or face any problems, please contact us using the Contact Form or leave a comment. We will do our best to solve it together. 🤝 | ⭐ Follow us for new updates and keep learning! Introduction Variables and Data Types are the fundamental concepts of Python programming. Variables are used to store data, while Data Types define the kind of data a variable can hold. These concepts form the foundation of every Python program, making code simple, organized, efficient, and easy to understand and maintain. Roman English: Variables data store karte hain, aur Data Types batate hain data kis type ka hai. Ye Python programming ki basic foundation hain. Learning Objectives After completing this topic, you will be able to: Underst...

Python Print Statement and Comments Explained with Examples

1) Print Statement Function in Python A print statement Function is used to show output on the screen. In Python, we write print() with a small letter p, not a capital letter. Roman : Print statement Function screen par output dikhane ke liye use hota hai. Python me print() small letter p se likhte hain, kyunki Python case-sensitive hota hai. Example of Print Statement Function in Python : print("Hello World") Output(show on Terminal): Hello World Another Example of Print Statement Function in Python : print(10) print(2.0) print(True) print(10+5) print(2*5) print(0-0) Output(show on Terminal): 10 2.0 True 15 10 0 2) Comments in Python (Single and Multiples): Single Line Comments using Hash(#) in Python A comment is used to explain a line of code. In Python, a comment is written using #(hash...