Chapter 05
Chapter 05
An Introduction to
Computer Science
Chapter 5
Sequences: Strings, Lists, and Files
H e l l o B o b
0 1 2 3 4 5 6 7 8
>>> greet = "Hello Bob"
>>> greet[0]
'H'
>>> print(greet[0], greet[2], greet[4])
H l o
>>> x = 8
>>> print(greet[x - 2])
B
S p a m !
>>>
Please enter your first name (all lowercase): donna
Please enter your last name (all lowercase): rostenkowski
uname = drostenk
def main():
def main():
def main():
# months is a list used as a lookup table
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
n = int(input("Enter a month number (1-12): "))
def main():
print("This program converts a textual message into a sequence")
print ("of numbers representing the Unicode encoding of the message.\n")
# Loop through the message and print out the Unicode values
for ch in message:
print(ord(ch), end=" ")
def main():
print ("This program converts a sequence of Unicode numbers into")
print ("the string of text that it represents.\n")
--------------------------------------------------------------------------
This program converts a sequence of Unicode numbers into
the string of text that it represents.
Please enter the ASCII-encoded message: 67 83 49 50 48 32 105 115 32 102 117 110 33
The decoded message is: CS120 is fun!
def main():
print("This program converts a sequence of Unicode numbers into")
print("the string of text that it represents.\n")
message = "".join(chars)
print("\nThe decoded message is:", message)
def main():
print ("Change Counter\n")
Please enter the count of each coin type. Please enter the count of each coin type.
Quarters: 0 Quarters: 12
Dimes: 0 Dimes: 1
Nickels: 0 Nickels: 0
Pennies: 1 Pennies: 4
The total value of your change is $0.01 The total value of your change is $3.14
def main():
fname = input("Enter filename: ")
infile = open(fname,'r')
data = infile.read()
print(data)
◼ First, prompt the user for a file name
◼ Open the file for reading
◼ The file is read as one string and stored in the
variable data
def main():
print ("This program creates a file of usernames from a")
print ("file of names.")