Contents

  1. ascii.py
  2. ascii_table.py
  3. binaryToDecimal2.py
  4. search.py
  5. string_compare.py
  6. string_index.py
  7. string_methods.py
  8. whilestr.py

ascii.py 1/8

[
top][prev][next]
# Conversion of a text message into ASCII
# by Sara Sprenkle, 10.03.2007

print "This program converts a textual message into a sequence"
print "of numbers representing the ASCII encoding of the message."
print

message = raw_input("Please enter the message to encode: ")

print
print "Here are the ASCII codes:"

for ch in message:
    print ord(ch),

print

ascii_table.py 2/8

[
top][prev][next]
# Create an ASCII table
# by Sara Sprenkle, 10.02.07

print "DEC CHAR"
print "-"*3, "-"*4

for i in xrange(33, 127):
    print "%3d %4s" % (i, chr(i))

binaryToDecimal2.py 3/8

[
top][prev][next]
# Convert binary numbers to decimal numbers
# Alternative solution proposed by Cathy
# by Sara Sprenkle, 10.03.2007

print 
print "This program converts binary numbers to decimal numbers."
print

binary_string = raw_input("Enter a number in binary: ")

dec_value = 0

for exponent in xrange( len(binary_string)):
    bit = int(binary_string[-(exponent+1)])
    # alternatively
    #bit = int(binary_string[ len(binary_string) - 1 - exponent ] )
    print bit,"* 2^%d" % exponent
    dec_value += bit * (2 ** exponent)

print "The decimal value is", dec_value

search.py 4/8

[
top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
#

# QUESTION: Why is this a constant?
PYTHON_EXT = ".py"

filename = raw_input("Enter a filename: ")

if filename[-(len(PYTHON_EXT)):] == PYTHON_EXT:
    print "That's a name for Python script"

if PYTHON_EXT in filename:
    print "That filename contains", PYTHON_EXT

# QUESTION: SHOULD THIS BE AN IF/ELIF ?

string_compare.py 5/8

[
top][prev][next]
# Program compares two strings
# by Sara Sprenkle, 10.04.2007

str1 = raw_input("Enter a string to compare: ")
str2 = raw_input("Compare " + str1 + " with what string? ")

print "-------------------------"

if str1 < str2 :
    print "Alphabetically, ", str1, "comes before", str2
else :
    print "Alphabetically, ", str2, "comes before", str1

string_index.py 6/8

[
top][prev][next]
# Iterating through a string
# by Sara Sprenkle, 10.01.2007

print
str = raw_input("Enter a string to iterate through: ")
print

header1 = "index"
header2 = "character"

print header1, header2
print "-"*len(header1), "-"*len(header2)

i=0

while i < len(str) :
    print "%5d %9s" % (i, str[i])
    i+=1


string_methods.py 7/8

[
top][prev][next]
# Do stuff to strings, using methods
# by Sara Sprenkle, 10.05.2007

str = raw_input("Enter a sentence to mangle: ")

length = len(str)

# Question: What does the statement below do?
print "*", str.center(int(length*1.5)), "*"

print "Uppercase: ", str.upper()

print "Lowercase: ", str.lower()

# Answer before running...
print "Did str change?: ", str

whilestr.py 8/8

[
top][prev][next]
# Iterating through a string
# by Sara Sprenkle, 10.01.2007

print
str = raw_input("Enter a string to iterate through: ")
print

header1 = "index"
header2 = "character"

print header1, header2
print "-"*len(header1), "-"*len(header2)

i=0

while i < len(str) :
    print "%5d %9s" % (i, str[i])
    i+=1


Generated by GNU enscript 1.6.4.