Contents
- ./demo_str.py
- ./escape_sequences.py
- ./get_num_letters_case_insensitive.py
- ./search.py
- ./string_compare.py
- ./string_methods.py
- ./test.py
./demo_str.py 1/7
[top][prev][next]
# Demonstrate long strings and escape sequences
# by Sara Sprenkle
string = """This is a long string.
Like, really long.
Sooooo loooooong"""
print(string)
print("\n") # how many blank lines does this print?
print("To print a \\, you must use \"\\\\\"")
./escape_sequences.py 2/7
[top][prev][next]
# Practice with escape sequences
# By CS111
# Display To print a tab, you must use '\t'.
print("To print a tab, you must use \'\\t\'.")
print("To print a tab, you must use '\\t'.")
print('To print a tab, you must use \'\\t\'.')
# how many blank lines does this print?
print("\n")
# Display I said, "How are you?"
print("I said, \"How are you?\"")
print('I said, \"How are you?\"')
print('I said, "How are you?"')
./get_num_letters_case_insensitive.py 3/7
[top][prev][next]
# Finds the number of times a letter appears in a phrase, case insensitive.
# Practice using the str API
# by CSCI111
import test
def get_num_letters(phrase, letter):
"""
Parameters:
- phrase: a string phrase
- letter: a single letter
returns the number of times the letter appears in
the phrase, case insensitive
"""
lower_letter = letter.lower()
lower_phrase = phrase.lower()
num_occurrences = lower_phrase.count(lower_letter)
return num_occurrences
def test_get_num_letters():
test.testEqual( get_num_letters("abracadabra", "a"), 5)
test.testEqual( get_num_letters("Abracadabra", "a"), 5)
test.testEqual( get_num_letters("abracadabra", "A"), 5)
test_get_num_letters()
./search.py 4/7
[top][prev][next]
# Demonstrate use of "in" operator for strings as well
# as an if test
# Sara Sprenkle
PYTHON_EXT = ".py"
filename = input("Enter a filename: ")
if PYTHON_EXT in filename:
print "That filename contains", PYTHON_EXT
if filename[-(len(PYTHON_EXT)):] == PYTHON_EXT:
print "That's a name for Python script"
# QUESTION: SHOULD THIS BE AN IF/ELIF?
# What is the impact of that change?
./string_compare.py 5/7
[top][prev][next]
# Program compares two strings
# by Sara Sprenkle
str1 = input("Enter a string to compare: ")
str2 = input("Compare '" + str1 + "' with what string? ")
print("-" * 40)
if str1 < str2 :
print("Lexicographically,", str1, "comes before", str2 + ".")
elif str1 > str2:
print("Lexicographically,", str2, "comes before", str1 + ".")
else:
print("You tried to trick me!", str1, "and", str2, "are the same word!")
./string_methods.py 6/7
[top][prev][next]
# Manipulate strings, using methods
# by Sara Sprenkle
sentence = input("Enter a sentence to mangle: ")
length = len(sentence)
# Question: What does the statement below do?
print("*", sentence.center(int(length*1.5)), "*")
upperSentence = sentence.upper()
print(upperSentence)
print(sentence)
print("Uppercase: ", sentence.upper())
print()
print("Lowercase: ", sentence.lower())
print()
# Answer before running...
print("Did sentence change?: ", sentence)
sentence = sentence.lower()
print(sentence)
./test.py 7/7
[top][prev][next]
def testEqual(actual,expected,places=5):
'''
Does the actual value equal the expected value?
For floats, places indicates how many places, right of the decimal, must be correct
'''
if isinstance(expected,float):
if abs(actual-expected) < 10**(-places):
print('\tPass')
return True
else:
if actual == expected:
print('\tPass')
return True
print('\tTest Failed: expected {} but got {}'.format(expected,actual))
return False
Generated by GNU Enscript 1.6.5.90.