Contents
- ./demo_str.py
- ./escape_sequences.py
- ./search.py
- ./string_compare.py
- ./string_methods.py
./demo_str.py 1/5
[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/5
[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?"')
./search.py 3/5
[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 4/5
[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 5/5
[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)
Generated by GNU Enscript 1.6.5.90.