Input and Output in Python Class 11 CBSE Computer Science: input(), print(), sep, end and Escape Sequences
Class 11 · Computer Science
Input and Output in Python (CBSE Class 11 Computer Science)
Every Python program interacts with the user by accepting input and displaying output. The input() function is used to receive data from the user, while the print() function displays information on the screen. Understanding these functions is essential for writing interactive Python programs.
Learning Objectives
- Understand Input and Output.
- Learn the
input()function. - Learn the
print()function. - Understand escape sequences.
- Use
sepandendparameters. - Display formatted output.
Input and Output
User
│
▼
input()
│
Process
│
▼
print()
│
▼
Output on Screen
Input Function
The input() function is used to accept data from the keyboard. Whatever the user enters is returned as a string.
input() function accepts input from the user and returns it as a string.
Syntax
variable = input("Message")
Example 1
name = input("Enter your name: ")
print(name)
Sample Output
Enter your name: Aman
Aman
Example 2: Numeric Input
age = int(input("Enter your age: "))
print(age)
Since input() returns a string, int() converts it into an integer.
Output Function
The print() function displays messages, variables, and results on the screen.
Syntax
print(value1, value2, ...)
Example
name = "Riya"
marks = 95
print(name)
print(marks)
Output
Riya
95
Printing Multiple Values
name = "Riya"
marks = 95
print(name, marks)
Output
Riya 95
The sep Parameter
The sep parameter specifies the separator between multiple values printed by the print() function.
Syntax
print(value1, value2, sep="separator")
Example
print("Python", "CBSE", "Class 11", sep=" | ")
Output
Python | CBSE | Class 11
The end Parameter
By default, the print() function moves the cursor to the next line after displaying the output. The end parameter changes this behavior.
Syntax
print(value, end="...")
Example
print("Python", end=" ")
print("Programming")
Output
Python Programming
Escape Sequences
Escape sequences are special character combinations beginning with a backslash (\) that perform special functions while displaying output.
| Escape Sequence | Description |
|---|---|
| \n | New Line |
| \t | Horizontal Tab |
| \\ | Backslash |
| \' | Single Quote |
| \" | Double Quote |
Examples of Escape Sequences
New Line (\n)
print("Python\nProgramming")
Output
Python
Programming
Tab (\t)
print("Name\tMarks")
Output
Name Marks
Printing Quotes
print("He said \"Hello\"")
Output
He said "Hello"
Formatted Output Using f-Strings
Python provides formatted string literals (f-strings) to insert variable values directly into a string. This feature is available in Python 3.6 and later.
Syntax
name = "Aman"
marks = 95
print(f"{name} scored {marks} marks.")
Output
Aman scored 95 marks.
Dry Run Example
Program
name = input("Enter Name: ")
age = int(input("Enter Age: "))
print(name, age)
Dry Run Table
| Step | Operation | Value |
|---|---|---|
| 1 | User enters Name | Aman |
| 2 | Stored in name | "Aman" |
| 3 | User enters Age | 17 |
| 4 | int() converts to integer | 17 |
| 5 | print() | Aman 17 |
Think Like a Programmer
Predict the output.
print("A", "B", "C", sep="-")
print("Python", end=" ")
print("Programming")
Click to View Answer
A-B-C
Python Programming
Output Prediction Challenge
name = "Riya"
marks = 92
print(name, marks, sep=" : ")
print("Result", end=" => ")
print("Pass")
Click to View Answer
Riya : 92
Result => Pass
Real-Life Example
Consider an ATM machine:
- The ATM asks for your PIN → Input
- The ATM processes your request.
- The ATM displays your account balance → Output
Similarly, Python programs accept input, process it, and display output.
Common Beginner Mistakes
- Forgetting that
input()returns a string. - Not converting numeric input using
int()orfloat(). - Confusing
sepandend. - Using incorrect escape sequences.
- Missing quotation marks in
print()statements.
Visual Memory Box
| Function / Parameter | Purpose |
|---|---|
input() | Accept user input |
print() | Display output |
sep | Separator between values |
end | Ending character after output |
\n | New line |
\t | Horizontal tab |
Memory Trick
- input() → Accept Data
- print() → Display Data
- sep → Separate Values
- end → End of Output
- \n → New Line
- \t → Tab Space
Exam Tips
- Remember that
input()always returns a string. - Use
int()orfloat()for numeric calculations. - Practice using
sepandend. - Learn the commonly used escape sequences.
- Know the syntax of both
input()andprint().
CBSE Important Questions
2 Marks
- Differentiate between
input()andprint(). - What is the purpose of the
sepparameter?
3 Marks
- Explain escape sequences with suitable examples.
- Write a program to accept a student's name and marks and display them using
sep.
5 Marks
- Explain the
input()andprint()functions with syntax, examples, escape sequences, and formatted output.
Practice Yourself
- Write a program to accept your name and city and display them.
- Write a program to accept two numbers and display their sum.
- Display a table using the
\tescape sequence. - Use
septo print values separated by a comma. - Use
endto print multiple words on the same line.
Frequently Asked Questions (FAQs)
1. What does the input() function return?
It always returns a string.
2. Which function is used to display output?
print().
3. What is the purpose of the sep parameter?
It specifies the separator between multiple values printed by print().
4. What is the default value of the end parameter?
The default value is \n (new line).
5. Which escape sequence inserts a new line?
\n.
Summary
input()accepts data from the user and returns it as a string.print()displays output on the screen.- The
sepparameter changes the separator between printed values. - The
endparameter changes how aprint()statement ends. - Escape sequences such as
\nand\thelp format output. - Input and output functions are used in almost every Python program.