CSCI 1301 Lecture Notes - Lecture 3: Test Case

43 views3 pages

Document Summary

Each character in a string (or item in a list) has an index position! You can figure out how many characters are in a string (or items in a list) using the len() function! len("elephant") = 8. We can use these index positions to get the value of an item at a particular position in a string or list. Values e l e p h a n t. 7 word = "elephant" word[1] = "l" word[5] = "a" Trace this function line-by-line: def practice(phrase): p = "" for each in range(0,len(phrase),2): p = p + phrase[each] print p. Slicing also uses the index positions of characters (or items) to return a subsection of the items. The format for slicing is variable [ start : stop : step ] Variable: the variable where the characters or items are saved. This is similar to using the range function, but with square brackets [ ] and colons : word = "helloworld .

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions