Difference between revisions of "CSC111 Lab 6"
(→Massive replacement) |
|||
Line 72: | Line 72: | ||
and the replacement words are in this list: | and the replacement words are in this list: | ||
− | newWords = ["carrot", "chunk", "elbow", "melt"] | + | newWords = ["carrot", "chunk", "elbow", "melt"] |
Write some python code that will use '''sentence''' and replace '''chocolate''' by '''carrot''', then it will look for '''piece''' and replace it by '''chunk''', '''hand''' by '''elbow''', and '''break''' by '''melt'''. Use a for-loop! | Write some python code that will use '''sentence''' and replace '''chocolate''' by '''carrot''', then it will look for '''piece''' and replace it by '''chunk''', '''hand''' by '''elbow''', and '''break''' by '''melt'''. Use a for-loop! | ||
− | |||
===Count()=== | ===Count()=== |
Revision as of 22:26, 2 March 2010
This lab is under construction...
Contents
More String Methods
We have already seen several of these methods. This lab is to reinforce our knowledge of strings.
The methods are all documented at this page: http://docs.python.org/lib/string-methods.html. Open this page while you working on this lab.
center()
One of the string methods is the center function. Use it to center the title "computer SCIENCE 111" between two lines of 45 dashes. Use a variable called myTitle to store the string.
myTitle = "computer SCIENCE 111" print 45*"-" print myTitle.center( 45 ) print 45*"-"
capitalize()
Instead of printing the variable myTitle, print myTitle.capitalize( ) and see what happens.
upper()
Use upper() instead of capitalize(). Observe the new output.
title()
Same exercise, but with the title() method. How can you make the program display the lowercase version of your title without your modifying the myTitle variable?
- Programming Question
- Write a program that uses functions, that asks for your name, then your last name, and prints both of them centered between two lines of 60 dashes. Make your program capitalize the first letter of each word.
- Example
your first name? alexAndra your last name? SMITH ------------------------------------------------------------ Alexandra Smith ------------------------------------------------------------
Replacing substrings in a strings
To replace a substring of a string (or a word in a sentence), all you need to do is to use the replace() method.
sentence = """Strength is the capacity to break a chocolate bar into four pieces with your bare hands - and then eat just one of the pieces""" print sentence sentence = sentence.replace( "chocolate", "carrot" ) print sentence
Try it and replace the word "piece" by the word "chunk" in the same sentence, so that now you're breaking carrots into chunks.
Massive replacement
We are still dealing with the sentence above, but we want to replace four separate words by four new words. The words we want to replace are in this list:
toBeReplaced = ["chocolate", "piece", "hand", "break"]
and the replacement words are in this list:
newWords = ["carrot", "chunk", "elbow", "melt"]
Write some python code that will use sentence and replace chocolate by carrot, then it will look for piece and replace it by chunk, hand by elbow, and break by melt. Use a for-loop!
Count()
Read the documentation on the method count() and write a program or a function that outputs the number of times the word "piece" appears in sentence using the count() method.
Splitting strings
sentence = """Strength is the capacity to break a chocolate bar into four pieces with your bare hands - and then eat just one of the pieces"""
print sentence words = sentence.split() print words
Add the code above to your program. Try it.
Make your program print the number of words in the sentence (there are several ways to do that: one long, one very short).
Make your program display the first and last words of the sentence.