Objective: Console Input and Output
Objective: Console Input and Output
LAB # 03
CONSOLE INPUT AND OUTPUT
OBJECTIVE
Taking input from user and controlling output position.
THEORY
Console I/O Functions
The keyboard and visual display unit (VDU) together are called a console. Python
programming language provides many built-in functions to read any given input and to
display data on screen, Console (also called Shell) is basically a command line
interpreter that takes input from the user i.e one command at a time and interprets it. If
it is error free then it runs the command and gives required output otherwise shows the
error message.
Syntax : input(prompt)
The message can be a string, or any other object, the object will be converted into a
string before written to the screen.
Example:
name=input('Please enter your name: "')
print("Hello, " , name , "!")
Output:
>>> %Run task1.py
Please enter your name:ABC
Hello, ABC!
>>>
1
Whatever you enter as input, input function convert it into a string. if you enter an
integer value still input() function convert it into a string. You need to explicitly convert
it into an integer in your code using typecasting.
Example:
# Program to check input
num = input ("Enter number
:") print(num)
name1 = input("Enter name : ")
print(name1)
We can also type cast this input to integer, float or string by specifying the input()
function inside the type.
Typecasting the input to Integer/Float: There might be conditions when you might
require integer input from user/console, the following code takes two
input(integer/float) from console and typecasts them to integer then prints the sum.
Example
# input
num1 = int(input())
num2 = int(input())
Escape Sequence
In Python strings, the backslash "\" is a special character, also called the "escape"
character. An escape sequence is a sequence of characters that does not represent itself
when used inside a character or string literal, but is translated into another character
or a sequence of characters that may be difficult or impossible to represent directly.
Escape
Description Example Output
Sequence
\\ Prints Backslash print ("\\") \
\` Prints single-quote print ("\'") '
\" Pirnts double quote print ("\"") "
print hello
\n ASCII linefeed ( LF )
("hello\nworld") world
ASCII backspace ( BS ) removes print ("az" + "\b"
\b ac
previous character + "c")
ASCII horizontal tab (TAB). Prints
\t print ("\t*hello") *hello
TAB
EXERCISE
1. print("Hello \b World!")
3. age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
1. a=5
print("a =", a, sep='0', end=',')
2. name = input("Enter Employee
Name") salary = input("Enter
salary") company = input ("Enter
Company name") print("Printing
Employee Details") print ("Name",
"Salary", "Company") print (name,
salary, company)
3. n1=int(input('"enter n1 value'))
n2=int(input('enter n2 value'))
1. Write a program to print a student’s bio data having his/her Date of birth, Roll no,
Section, Percentage and grade of matriculation and Intermediate. All the fields
should be entered from the console at run time.
2. Write a program that asks the user what kind of food they would like. Print a
message about that food, such as “Let me see if I can find you a Chowmein”. Food
name must be in uppercase. (hint: use upper( ) for food name)
3. Take the marks of 5 courses from the user and calculate the average and
percentage, display the result:
Eachcourse=50 marks
Total_marks= course1+course2+course3+course4+course5
average=Total_marks/5
percentage=(Total_marks x 100)/250