Chapter 8, A Detailed Look at the printf and scanf Functions

Program 8-1 , Page number: 149

In [1]:
# A print demo
# The conversion specification for numbers.

print "Case  1 :%d:"%123

print "Case  2 :%0d:"%123

print "Case  3 :%2d:"%123

print "Case  4 :%8d:"%123

print "Case  5 :%-8d:"%123

print "Case  6 :%8d:"%-123

print "Case  7 :%f:"%123.456

print "Case  8 :%f:"%123.4567896

print "Case  9 :%e:"%123.456

print "Case  10 :%e:"%1.23456e2

print "Case  11 :%e:"%123.456e+0

print "Case  12 :%f:"%1.23456E+02

print "Case  13 :%4.2f:"%123.456

print "Case  14 :%8.2f:"%123.456

print "Case  15 :%.3e:"%123.456

print "Case  16 :%12.3e:"%123.456
Case  1 :123:
Case  2 :123:
Case  3 :123:
Case  4 :     123:
Case  5 :123     :
Case  6 :    -123:
Case  7 :123.456000:
Case  8 :123.456790:
Case  9 :1.234560e+02:
Case  10 :1.234560e+02:
Case  11 :1.234560e+02:
Case  12 :123.456000:
Case  13 :123.46:
Case  14 :  123.46:
Case  15 :1.235e+02:
Case  16 :   1.235e+02:

Program 8-2 , Page number: 152

In [2]:
# A print demo:
# Interpretation of values

# python is dynamically-typed language
# which is why declaration of variables is omitted
n = -123
c = "V"

print "%d"%n
print "%u"%n		# python correctly interprets the negative(unsigned) integer

print "%c"%c
# print "%d"%c  	# python doesn't support implicit type-casting from integer to character or character to integer
print "%c"%86
-123
-123
V
V

Program 8-3 , Page number: 153

In [3]:
# Printing out the ASCII value of a character

# There is no do..while loop in python
# Hence, we will be using while loop 


answer = "y"
while answer == "y" :
	print "Enter a character:",
	in_char = "a"
	print in_char

	print "The ASCII value of %c is %d"%(in_char,ord(in_char)) 	# ord() function returns the ascii(int) value of a character

	print "\nAnother? (y/n)",
	answer = "n"			# To terminate the loop
	print answer
Enter a character: a
The ASCII value of a is 97

Another? (y/n) n

Program 8-4 , Page number: 154

In [4]:
# Converting a digit to a number by using its ASCII value

print "Enter a digit:",
digit = "A"
print digit

if digit < "0" or digit > "9" :
	print "That's not a digit"
else :
	print "The digit you typed is %c"%digit
Enter a digit: A
That's not a digit

Program 8-5 , Page number: 156

In [5]:
# Converting a letter from lower to upper case or vice-versa

answer = "y"
while answer == "y" :
	print "\n\n\nEnter a letter:",
	letter = "A"
	print letter

	if letter >= "A" and letter < "Z" :		# Letter is uppercase
		print "Lower case is: %c"%chr(ord(letter) + (ord("a")-ord("A")))
	elif letter >= "a" and letter <= "z" :	# Letter is lowercase
		print "Upper case is: %c"%chr(ord(letter) - (ord("a")-ord("A")))
	else :
		print "That's not a letter"

	# Ask if the user wants to go again
	print "\nAnother? (y/n)",
	answer = "n"		# To terminate the loop
	print answer


Enter a letter: A
Lower case is: a

Another? (y/n) n

Program 8-6 , Page number: 157

In [6]:
# Example of using control characters

print "The word apple\b\b\b\b\b_____ is underlined.\n"
print "The last word in this sentence is way over\t\there."
The word apple_____ is underlined.

The last word in this sentence is way over		here.

Program 8-7 , Page number: 158

In [7]:
# How to print special characters

print "This is how to print a double-quote: \""
print "This is how to print a backslash: \\"
# this is how to write an apostrophe character
c = '\''
print "c now contains: %c"%c
print "This is how to print a percent-sign: %"		# no escape sequence required for % in python
This is how to print a double-quote: "
This is how to print a backslash: \
c now contains: '
This is how to print a percent-sign: %

Program 8-8 , Page number: 160

In [8]:
# Octal and hexadecimal notation

a = 27
b = 033
c = 0x1b

print "a = %d, b = %d, c = %d"%(a,b,c)
print "a = %o, b = %o, c = %o"%(a,b,c)
print "a = %x, b = %x, c = %x"%(a,b,c)
a = 27, b = 27, c = 27
a = 33, b = 33, c = 33
a = 1b, b = 1b, c = 1b

Program 8-9 , Page number: 162

In [9]:
# Illustration of some ways to print a string

print "This is simply a control string."

print "%s"%("The control string just tells how to print this.")

print "%s"%("Newline may be put here, instead!.\n"),

print "%s %s %s %s %s"%("This","is","valid","in","C.")

print "%s %s also %s %s %s"%("This","is","valid","in","C.")

print "%s %s %s %s %s %s %s"%("This","is","valid","in","C","also.","\n"),
This is simply a control string.
The control string just tells how to print this.
Newline may be put here, instead!.
This is valid in C.
This is also valid in C.
This is valid in C also. 

Program 8-10 , Page number: 163

In [10]:
# %s conversion specification demonstrated

print "Case 1 :%s:"%("A string")

print "Case 2 :%3s:"%("A string")

print "Case 3 :%12s:"%("A string")

print "Case 4 :%-12s:"%("A string")

print "Case 5 :%12.6s:"%("A string")

print "Case 6 :%12.12s:"%("A string")

print "Case 7 :%.6s:"%("A string")

print "Case 8 :%-12.6s:"%("A string")
Case 1 :A string:
Case 2 :A string:
Case 3 :    A string:
Case 4 :A string    :
Case 5 :      A stri:
Case 6 :    A string:
Case 7 :A stri:
Case 8 :A stri      :

Program 8-11 , Page number: 164

In [11]:
# Some miscellaneous conversion sppecifications

a = 123456789L

print "%d"%a			# in python both int and long are unified..so %d specifier valid unlike in C
print "%ld"%a
print ":%c:"%("V")
print ":%0c:"%("V")
print ":%5c:"%("V")
print ":%-5c:"%("V")
123456789
123456789
:V:
:V:
:    V:
:V    :

Program 8-13 , Page number: 171

In [12]:
# Reading decimal,octal and hex numbers

print "Enter a number in decimal:",
a = 27
print a
print "You entered %d"%a

print "\nEnter a number in octal:",
a = 033			# O is added in the front to specify octal value
print a
print "You entered %o, or %d in decimal"%(a,a)

print "\nEnter a number in hex:",
a = 0x1B
print a
print "You entered %x, or %d in decimal"%(a,a)
Enter a number in decimal: 27
You entered 27

Enter a number in octal: 27
You entered 33, or 27 in decimal

Enter a number in hex: 27
You entered 1b, or 27 in decimal