Difference between revisions of "CSC111 Lab 4 2015"
Line 17: | Line 17: | ||
Use it to center the title "computer SCIENCE 111" between two lines of 45 dashes. Use a variable called myTitle to store the string. | Use it to center the title "computer SCIENCE 111" between two lines of 45 dashes. Use a variable called myTitle to store the string. | ||
− | + | <br /> | |
+ | ::<source lang="python"> | ||
myTitle = "computer SCIENCE 111" | myTitle = "computer SCIENCE 111" | ||
− | print 45*"-" | + | print( 45*"-" ) |
− | print myTitle.center( 45 ) | + | print( myTitle.center( 45 ) ) |
− | print 45*"-" | + | print( 45*"-" ) |
− | + | </source> | |
+ | < br /> | ||
===capitalize()=== | ===capitalize()=== | ||
− | + | < br /> | |
Instead of printing the variable myTitle, print myTitle.'''capitalize'''( ) and see what happens. | Instead of printing the variable myTitle, print myTitle.'''capitalize'''( ) and see what happens. | ||
− | + | < br /> | |
===upper()=== | ===upper()=== | ||
− | + | < br /> | |
Use '''upper'''() instead of capitalize(). Observe the new output. | Use '''upper'''() instead of capitalize(). Observe the new output. | ||
− | + | < br /> | |
===title()=== | ===title()=== | ||
− | + | < br /> | |
Same exercise, but with the '''title'''() method. | Same exercise, but with the '''title'''() method. | ||
+ | < br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ===Challenge #1=== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
− | + | *Write some Python code that will ask for your name, then your last name, and will print both of them centered between two lines of 60 dashes. Make your program capitalize the first letter of each word. | |
− | + | ;Example | |
− | + | <br /> | |
− | + | <source lang="text"> | |
your first name? alexAndra | your first name? alexAndra | ||
your last name? SMITH | your last name? SMITH | ||
Line 47: | Line 56: | ||
Alexandra Smith | Alexandra Smith | ||
------------------------------------------------------------ | ------------------------------------------------------------ | ||
− | + | <br /> | |
===Replacing substrings in a string=== | ===Replacing substrings in a string=== | ||
− | + | <br /> | |
To replace a substring of a string (or a word in a sentence), all you need to do is to use the '''replace()''' method. | To replace a substring of a string (or a word in a sentence), all you need to do is to use the '''replace()''' method. | ||
− | + | <br /> | |
+ | ::<source lang="python"> | ||
+ | # we define a multi-line string with triple-double-quotes | ||
sentence = """Strength is the capacity to break a chocolate | sentence = """Strength is the capacity to break a chocolate | ||
bar into four pieces with your bare hands - and | bar into four pieces with your bare hands - and | ||
then eat just one of the pieces""" | then eat just one of the pieces""" | ||
− | print sentence | + | print( "Original sentence =", sentence ) |
sentence = sentence.replace( "chocolate", "carrot" ) | sentence = sentence.replace( "chocolate", "carrot" ) | ||
− | print sentence | + | print( "New sentence = ", sentence ) |
− | + | </source> | |
+ | <br /> | ||
Try it and replace the word "piece" by the word "chunk" in the same sentence, so that now you're breaking carrots into chunks. | Try it and replace the word "piece" by the word "chunk" in the same sentence, so that now you're breaking carrots into chunks. | ||
− | + | <br /> | |
===Multiple Replacements=== | ===Multiple Replacements=== | ||
− | + | <br /> | |
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: | 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: | ||
− | + | <br /> | |
+ | ::<source lang="python"> | ||
toBeReplaced = ["chocolate", "piece", "hand", "break"] | toBeReplaced = ["chocolate", "piece", "hand", "break"] | ||
− | + | </source> | |
+ | <br /> | ||
and the replacement words are in this list: | and the replacement words are in this list: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
− | newWords = ["carrot", "chunk", "elbow", "melt"] | + | newWords = [ "carrot", "chunk", "elbow", "melt" ] |
+ | </source> | ||
+ | <br /> | ||
Write python statements that will take the string '''sentence''' and replace '''chocolate''' by '''carrot''', then it will look for '''piece''' and replace it by '''chunk''', '''hand''' by '''elbow''', and '''break''' by '''melt'''. | Write python statements that will take the string '''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!''' | '''Use a for-loop!''' | ||
− | + | <br /> | |
===Count()=== | ===Count()=== | ||
− | + | <br /> | |
Read the documentation on the method '''count()''' and write some statements that output the number of times the word "piece" appears in '''sentence''' using the '''count()''' method. | Read the documentation on the method '''count()''' and write some statements that output the number of times the word "piece" appears in '''sentence''' using the '''count()''' method. | ||
− | + | <br /> | |
===Strip()=== | ===Strip()=== | ||
− | + | <br /> | |
Read the documentation about the method '''strip()''' and use it to allow you to print the lines below left aligned. | Read the documentation about the method '''strip()''' and use it to allow you to print the lines below left aligned. | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
listOfStrings = [ " But then, shall I never get any older than I am now? ", | listOfStrings = [ " But then, shall I never get any older than I am now? ", | ||
Line 88: | Line 108: | ||
" but then -- always to have lessons to learn! ", | " but then -- always to have lessons to learn! ", | ||
" Alice " ] | " Alice " ] | ||
− | + | </source> | |
+ | <br /> | ||
===Splitting strings=== | ===Splitting strings=== | ||
− | + | <br /> | |
First, read the documentation on the '''split()''' method. | First, read the documentation on the '''split()''' method. | ||
Let's use the same string as before. | Let's use the same string as before. | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
sentence = """Strength is the capacity to break a chocolate | sentence = """Strength is the capacity to break a chocolate | ||
Line 99: | Line 122: | ||
then eat just one of the pieces""" | then eat just one of the pieces""" | ||
+ | </source> | ||
+ | <br /> | ||
And process it that way | And process it that way | ||
− | + | <br /> | |
+ | ::<source lang="python"> | ||
+ | |||
list1 = sentence.split( "\n" ) | list1 = sentence.split( "\n" ) | ||
− | print "list1 = ", list1 | + | print( "list1 = ", list1 ) |
list2 = sentence.split( ) | list2 = sentence.split( ) | ||
− | print "list2 = ", list2 | + | print( "list2 = ", list2 ) |
list3 = sentence.split( 's' ) | list3 = sentence.split( 's' ) | ||
− | print "list3 = ", list3 | + | print( "list3 = ", list3 ) |
+ | |||
+ | </source> | ||
+ | <br /> | ||
;Programming Question 2 | ;Programming Question 2 | ||
Line 171: | Line 201: | ||
</pre></code> | </pre></code> | ||
You will have discovered that both '''a''' and '''b''' contain strings representing the name of files on a computer. | You will have discovered that both '''a''' and '''b''' contain strings representing the name of files on a computer. | ||
− | ; | + | < br /> |
− | : What is the string ''slice'' that will return the drive information for a (i.e. 'H:')? If you replace a by b, does your statement still return the drive for b? If not, figure out a way to take the slice that works independently of the string a, or b. | + | {| style="width:100%; background:silver" |
+ | |- | ||
+ | | | ||
+ | |||
+ | ===Challenge #1=== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * What is the string ''slice'' that will return the drive information for a (i.e. 'H:')? If you replace a by b, does your statement still return the drive for b? If not, figure out a way to take the slice that works independently of the string a, or b. | ||
+ | |||
+ | < br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ===Challenge #1=== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * What is the slice that will return the extension only of '''a''' (i.e. ''doc'')? If you replace a by b in your statement, do you get the extension of b? If not, figure out a way to write the slice that will work independently of the length of the file name. | ||
+ | |||
+ | < br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
− | + | ===Challenge #1=== | |
− | : | + | |} |
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
− | + | * Once you have figured out the answers to Question 2, write a simple Python program that asks the user for a file name, such as the ones we used for '''a''' or '''b''' above, and that prints out the same file name back, but with the extension replaced with "txt". | |
− | |||
::Example | ::Example |
Revision as of 12:35, 15 February 2015
--D. Thiebaut (talk) 17:39, 14 February 2015 (EST)
Preparation for Homework #5
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 and refer to it while you are working on this lab.
Use emacs or the python interactive prompt to test out different methods, as we did in class yesterday.
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*"-" )
< br />
capitalize()
< br /> Instead of printing the variable myTitle, print myTitle.capitalize( ) and see what happens. < br />
upper()
< br /> Use upper() instead of capitalize(). Observe the new output. < br />
title()
< br /> Same exercise, but with the title() method. < br />
Challenge #1 |
- Write some Python code that will ask for your name, then your last name, and will print 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
------------------------------------------------------------
<br />
===Replacing substrings in a string===
<br />
To replace a substring of a string (or a word in a sentence), all you need to do is to use the '''replace()''' method.
<br />
::<source lang="python">
# we define a multi-line string with triple-double-quotes
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( "Original sentence =", sentence )
sentence = sentence.replace( "chocolate", "carrot" )
print( "New sentence = ", 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.
Multiple Replacements
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 python statements that will take the string 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 some statements that output the number of times the word "piece" appears in sentence using the count() method.
Strip()
Read the documentation about the method strip() and use it to allow you to print the lines below left aligned.
listOfStrings = [ " But then, shall I never get any older than I am now? ", " That'll be a comfort, one way -- never to be an old woman -- ", " but then -- always to have lessons to learn! ", " Alice " ]
Splitting strings
First, read the documentation on the split() method.
Let's use the same string as before.
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"""
And process it that way
list1 = sentence.split( "\n" ) print( "list1 = ", list1 ) list2 = sentence.split( ) print( "list2 = ", list2 ) list3 = sentence.split( 's' ) print( "list3 = ", list3 )
- Programming Question 2
- Make your program print the number of words in sentence (there are several ways to do that: one long, one very short).
- Programming Question 3
- Make your program display the first and last words of the sentence.
- Programming Question 4
- Assume that you have a string of this type:
NYTBestSellers = """1. THE HELP, by Kathryn Stockett 2. WORST CASE, by James Patterson and Michael Ledwidge 3. THE LOST SYMBOL, by Dan Brown 4. POOR LITTLE BITCH GIRL, by Jackie Collins 5. WINTER GARDEN, by Kristin Hannah """
- Write the code that will take this string, process it, and output the information in a different format, shown below:
Kathryn Stockett The Help James Patterson and Michael Ledwidge Worst Case Dan Brown The Lost Symbol Jackie Collins Poor Little Bitch Girl Kristin Hannah Winter Garden
For this part of the lab, you want to have your Macs running in Mac OS X mode!
String Slices
- Use Python in interactive mode and try the following Python statements. Try to figure out the answers to the questions.
Python 2.6 (r26:66714, Nov 3 2009, 17:33:38)
[GCC 4.4.1 20090725 (Red Hat 4.4.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = "H:/English103/paper1.doc"
>>> a
>>> len( a )
>>> a[0 : 2]
>>> a[3 : 13]
>>> a[-3 : -1]
>>> a[-5 : -1]
>>> a[-5 : ] (no, I didn't forget the second number!)
>>> a[ : 3] (and no again, I didn't forget the first number!)
>>> a[3 : -3]
>>> b = "C:/My Documents/paper2.htm"
You will have discovered that both a and b contain strings representing the name of files on a computer. < br />
Challenge #1 |
- What is the string slice that will return the drive information for a (i.e. 'H:')? If you replace a by b, does your statement still return the drive for b? If not, figure out a way to take the slice that works independently of the string a, or b.
< br />
Challenge #1 |
- What is the slice that will return the extension only of a (i.e. doc)? If you replace a by b in your statement, do you get the extension of b? If not, figure out a way to write the slice that will work independently of the length of the file name.
< br />
Challenge #1 |
- Once you have figured out the answers to Question 2, write a simple Python program that asks the user for a file name, such as the ones we used for a or b above, and that prints out the same file name back, but with the extension replaced with "txt".
- Example
- Filename? C:/My Documents/paper02182010.doc
- C:/My Documents/paper02182010.txt
<showafterdate after="20150215 00:00" before="20150601 00:00">
Solution Programs
</showafterdate>