Chapter 11: Console Input/Output

Printf Example, Page number: 397

In [2]:
avg = 346 
per = 69.2

#Result
print "Average = %d\nPercentage = %f" %(avg, per )
Average = 346
Percentage = 69.200000

Format Specifications, Page number: 399

In [1]:
weight = 63

#Result
print "weight is %d kg" %( weight ) 
print "weight is %2d kg"%( weight ) 
print "weight is %4d kg" %( weight )
print "weight is %6d kg" %(weight ) 
print "weight is %-6d kg" %( weight )
weight is 63 kg
weight is 63 kg
weight is   63 kg
weight is     63 kg
weight is 63     kg

String Format Specifiers, Page number: 400

In [3]:
firstname1 = "Sandy" 
surname1 = "Malya" 
firstname2 = "AjayKumar" 
surname2 = "Gurubaxani" 

#Result
print  "%20s%20s" %( firstname1, surname1 )
print  "%20s%20s" %(firstname2, surname2 ) 
               Sandy               Malya
           AjayKumar          Gurubaxani

Escape Sequences, Page number: 401

In [4]:
print  "You\tmust\tbe\tcrazy\nto\thate\tthis\tbook" 
You	must	be	crazy
to	hate	this	book

Format Conversions, Page number: 403

In [5]:
ch = 'z' 
i = 125 
a = 12.55 
s = "hello there !"

#Result
print  "%c %d %f" %( ch, ord(ch), ord(ch )) 
print  "%s %d %f"%( s, ord(s[1]), ord(s[1]) )
print  "%c %d %f"%(i ,i, i ) 
print  "%f %d\n"%( a, a )
z 122 122.000000
hello there ! 101 101.000000
} 125 125.000000
12.550000 12

Sprintf Function, Page number: 404

In [6]:
i = 10 
ch = 'A'
a = 3.14 


#Result
print "%d %c %f" %(i, ch, a ) 
str = "%d %c %f" %(i, ch, a ) #sprintf
print  "%s" %(str )
10 A 3.140000
10 A 3.140000

Unformatted Input, Page number: 406

In [1]:
import msvcrt

print "Press any key to continue" 
#msvcrt.getch( ) # will not echo the character 
print  "Type any character" 
#ch = msvcrt.getche( ) # will echo the character typed 
ch = 'A'
print ch
#ch = input("Type any character")#getchar( ) will echo character, must be followed by enter key 
print "Type any character"
ch = 8
print ch
#ch = input( "Continue Y/N" ) #fgetchar( ) will echo character, must be followed by enter key
print "Continue Y/N" 
ch = 'N'
print ch
Press any key to continue
Type any character
A
Type any character
8
Continue Y/N
N

Unformatted Output, Page number: 407

In [7]:
ch = 'A'

#Result
print  ch #putch
print  ch #putchar
print  ch  #fputchar
print  'Z'  #putch
print  'Z'  #putchar
print  'Z'  #fputchar
A
A
A
Z
Z
Z

Unformatted String Input/Output, Page number: 408

In [9]:
print "Enter name"
footballer = "Jonty Rhodes"
print footballer

#Result
print  "Happy footballing!" 
print footballer 
Enter name
Jonty Rhodes
Happy footballing!
Jonty Rhodes
In [ ]: