python lab sheet

profileAsmah
lab_sheet_3_half_.docx

CS 360 LAB 3:

STRINGS, FUNCTIONS, AND METHODS

Objective: The purpose of this lab is to get you comfortable with using the Python functions and modules.

Note: Python has a lot of modules that come with the language, which collectively are called the Python Standard Library. The Python documentation contains the specifications for all the functions in the library. You can get to it from the link on the course website, or by going here:

http://docs.python.org/library/

1) Strings

Strings have many handy methods, whose specifications can be found at the following URL:

http://docs.python.org/2/library/stdtypes.html#string-methods

Strings are a built-in type, so although there is a module named string, you do not need it for basic string operations.

To reference substrings (or even individual characters) we can use the bracket notation shown in class. Remember Python counts characters starting from zero|for example, s[1] is the second character in s.

One of the things to remember about strings is that they are immutable. Methods or sequence operations return new strings; they never modify the original string object.

Evaluating string Expressions. Before starting with the table below, enter the following assignment statement into the Python shell:

s = "Hello World!"

Once you have done that, you will use the string stored in s to _ll out the table below.

Expression Expected

Expected Value

Calculated value

Reason for calculated value

s[2]

s[15]

s[1:5]

s[:5]

s[5:]

"e" in s

"x" in s

s.index("e")

s.index("x")

s.index("l", 5)

where to start looking)

s.find("e")

s.find("x")

s.islower()

s[1:5].islower()