Difference between revisions of "CSC111 Lab 8 2014"
(Created page with "--~~~~ ---- <bluebox> This lab deals with strings and list operations, and transforming strings into lists and lists into strings. </bluebox> <br /> =Splitting Strings= <br /...") |
(→Splitting Strings) |
||
Line 26: | Line 26: | ||
>>> words[-2] | >>> words[-2] | ||
− | >>> chunks = line.split( ',' ) <font color=" | + | >>> chunks = line.split( ',' ) <font color="magenta"># split on commas</font> |
>>> chunks | >>> chunks | ||
− | >>> chunks = line.split( '.' ) <font color=" | + | >>> chunks = line.split( '.' ) <font color="magenta"># split on periods</font> |
>>> chunks | >>> chunks | ||
Line 35: | Line 35: | ||
>>> separator = "+" | >>> separator = "+" | ||
− | >>> newLine = separator.join( words ) <font color=" | + | >>> newLine = separator.join( words ) <font color="magenta"># join the words into a new string and use separator as the glue</font> |
>>> newLine | >>> newLine | ||
>>> separator = "$$$" | >>> separator = "$$$" | ||
− | >>> newLine = separator.join( words ) <font color=" | + | >>> newLine = separator.join( words ) <font color="magenta"># same but use $$$ as the glue</font> |
>>> newLine | >>> newLine | ||
− | >>> words <font color=" | + | >>> words <font color="magenta"># verify that you still have individual words in this list</font> |
− | >>> newWords = [ words[0], words[3], words[4], words[7], words[8], words[12] ] <font color=" | + | >>> newWords = [ words[0], words[3], words[4], words[7], words[8], words[12] ] <font color="magenta"># create a new list of some words</font> |
>>> newWords | >>> newWords | ||
− | + | ||
− | >>> " ".join( newWords ) <font color=" | + | >>> " ".join( newWords ) <font color="magenta"># join strings in newWords list with a space</font> |
Revision as of 13:02, 24 March 2014
--D. Thiebaut (talk) 14:01, 24 March 2014 (EDT)
This lab deals with strings and list operations, and transforming strings into lists and lists into strings.
Splitting Strings
Work in the console, and try these different commands. Observe what the different operations do.
>>> line = "The quick, red fox jumped. It jumped over the lazy, sleepy, brown dog." >>> line 'The quick, red fox jumped. It jumped over the lazy, sleepy, brown dog.' >>> line.split() >>> words = line.split() >>> words >>> words[0] >>> words[1] >>> words[-1]
>>> words[-2] >>> chunks = line.split( ',' ) # split on commas >>> chunks >>> chunks = line.split( '.' ) # split on periods >>> chunks >>> words >>> separator = "+" >>> newLine = separator.join( words ) # join the words into a new string and use separator as the glue >>> newLine >>> separator = "$$$" >>> newLine = separator.join( words ) # same but use $$$ as the glue >>> newLine >>> words # verify that you still have individual words in this list >>> newWords = [ words[0], words[3], words[4], words[7], words[8], words[12] ] # create a new list of some words >>> newWords >>> " ".join( newWords ) # join strings in newWords list with a space