Chapter 3: Basic Input-Output

Example getchar.c, page no. 79

In [1]:
c = raw_input("Type one character: ")
print "The character you typed is: %c" %c
Type one character: w
The character you typed is: w

Example gets.c, page no. 80

In [2]:
s = raw_input("Type a string (Less than 80 characters):")
print "The string typed is: %s" %s
Type a string (Less than 80 characters):C Book
The string typed is: C Book

Example 3.1, page no. 81

In [4]:
f_var = 10.12576893
print "Output 1: %f" %f_var
print "Output 2: %20f" %f_var
print "Output 3: %-20f" %f_var
print "Output 4: %+f" %f_var
wid_prec = int(raw_input("Input the width: "))
print "Output 5: %*f" %(wid_prec,f_var)
print "Output 6: %.5f" %f_var
wid_prec = int(raw_input("Input the precision: "))
print "Output 7: %20.*f" %(wid_prec,f_var)
print "Output 8: %-+20.5f" %(f_var)
Output 1: 10.125769
Output 2:            10.125769
Output 3: 10.125769           
Output 4: +10.125769
Input the width: 13
Output 5:     10.125769
Output 6: 10.12577
Input the precision: 8
Output 7:          10.12576893
Output 8: +10.12577           

Example scanf1.c, page no. 84

In [5]:
print "Input an integer followed by a floating-point number:"
int_var = int(raw_input("integer:"))
float_var=float(raw_input("float:"))
print "The Integer: %d" %int_var
print "The floating-point number: %f" %float_var
Input an integer followed by a floating-point number:
integer:34
float:56.57
The Integer: 34
The floating-point number: 56.570000

Example scanf2.c, page no. 85

In [1]:
#counting the inputs from user using raw_input() is not possible in Python. Hence, skipping the example

Example scanf3.c, page no. 86

In [1]:
print "Input the day, followed by the date (dd-mm-yyyy):"
day = raw_input("day:")
date = int(raw_input("date:"))
month = int(raw_input("month:"))
year = int(raw_input("year:"))
print "Day: %s" %day
print "Date: %s" %date
print "Month: %s" %month
print "Year: %s" %year
Input the day, followed by the date (dd-mm-yyyy):
day:Sunday
date:27
month:1
year:1974
Day: Sunday
Date: 27
Month: 1
Year: 1974

Example scanf4.c, page no. 87

In [7]:
print "Enter two strings: "
string1 = raw_input("string1:")
string2 = raw_input("string2:")
print "You have input:"
print string1
print string2
Enter two strings: 
string1:Hello
string2:world
You have input:
Hello
world

Example scanf5.c, page no. 88

In [5]:
#getting input from user the way it is given in textbook is not possible in Python.
#Hence, output will be different compared to textbook.

print "Input two string: "
string1 = raw_input("String 1: ")
string2 = raw_input("String 2: ")

print "The two strings are: %4s" %string1
print "The two strings are: %5s" %string2
Input two string: 
String 1: Morning
String 2: Comes
The two strings are: Morning
The two strings are: Comes

Example scanf6.c, page no. 89

In [7]:
print "Input the date:"
date = int(raw_input("date:"))
separator1 = raw_input("separator:")
month = int(raw_input("month:"))
separator2 = raw_input("separator:")
year = int(raw_input("year:"))
print "Date: %d" %date
print "Month: %d" %month
print "Year: %d" %year
Input the date:
date:31
separator:-
month:12
separator:/
year:1999
Date: 31
Month: 12
Year: 1999

Example scanf7.c, page no. 90

In [9]:
print "Input the date:"
date = int(raw_input("date:"))
month = int(raw_input("month:"))
year = int(raw_input("year:"))
print "Date: %d" %date
print "Month: %d" %month
print "Year: %d" %year
Input the date:
date:31
month:12
year:1999
Date: 31
Month: 12
Year: 1999