



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Files to submit: hw5.py Functions to include in hw5.py:
Look at the String methods in the Python documentation here: http://docs.python.org/lib/string-methods.html You might find these methods useful: lower() upper() startswith() Also, pay attention to string slicing: >>> text = "Yellow Jacket" >>> text[1] 'e' >>> text[1:] 'ellow Jacket' >>> text[0:3] 'Yel'
Include the answers to the following questions AS A COMMENT at the BOTTOM of your hw5.py file. Be sure to comment the answers out or your python code will NOT run. If your code does not run, you will receive a 0. All of the answers can be found by looking here: http://docs.python.org/lib/string-methods.html
The Python string method capitalize() returns a copy of the string with only its first character capitalized. We are going to write our own version of this built in method to understand better how it works. Write a function, myCapitalize() that takes in a single word as its parameter with the same functionality as the built in method, capitalize(). Sample Output: >>> "hello".capitalize() 'Hello' >>> "hELLo".capitalize() 'Hello' >>> myCapitalize("hello") 'Hello' >>> myCapitalize("hELLo") 'Hello' Note: The first two examples are using the built in method while the second two are using the function you write. You can check your function against the built in method in this way. Hint: You may find lower() and upper() to be helpful methods to use—you are most definitely allowed to use these.
We will write a simplified version of the startswith() method. (Look it up on the API for full functionality). Write a function, myStartsWith() that takes in two strings—a word and a prefix to look for in that word. If the prefix is the beginning of the word, return True. Otherwise, return False. Sample Output: >>> "Hi".startswith("h") False >>> "king".startswith("kings") False >>> "king".startswith("ki") True >>> "king".startswith("knight") False >>> myStartsWith("king", "kings") False >>> myStartsWith("king", "king") True >>> myStartsWith("king", "kong") False Hint: You may find string slicing (word[::]) and len() to be helpful.
Write a function called printMP3 that takes in a path and prints out all of the MP3’s located in that directory. If there are no MP3’s found, print “No MP3’s found!” As we have not gone over pathways in class yet, we are providing you with most of the code—the red commented out sections are where you need to add your own code. Here is a code skeleton to get you started: import os from os.path import isdir, isfile, join def printMP3(path): names = os.listdir(path) for name in names: longname = join(path, name) if isfile(longname) and #check to see if name ends with .mp3: #print out the file name
Sample Output: >>> printMP3("/Users/pythonwarrior/Desktop") 02 Mr. Brightside.mp 03 Smile Like You Mean It.mp 04 Somebody Told Me.mp 05 All These Things That I've Done.mp >>> printMP3("/Users/pythonwarrior") No MP3's found!
Pick another method from the Python API and write your own version of it.
Be sure to name your files and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written/Adapted by Beth York, Spring 2008