03 - Problem Solving With Python 2
03 - Problem Solving With Python 2
• strings
– Defining string type of data
– Searching through string / list
– Finding element in a string/ list
– Looping through string/ list
• List
– Creation
– Processing
– str
– len
– list
when constructing
index values and
slices
Module Code and Module Title Title of Slides
Strings Have Length
• There is a built-in
function len that b a n a n a
gives us the length 0 1 2 3 4 5
of a string
>>> fruit = 'banana'
>>> print(len(fruit))
6
'banana' len() 6
(a number)
(a string)
function
• Using a while
statement and an
iteration variable, and
the len function, we fruit = 'banana' 0b
can construct a loop index = 0 1a
while index < len(fruit) : 2n
to look at each of the letter = fruit[index] 3a
letters in a string print index, letter 4n
individually index = index + 1 5a
• A definite loop
using a for
statement is much
b
more elegant a
fruit = 'banana' n
• The iteration for letter in fruit : a
variable is print letter n
a
completely taken
care of by the for
loop
• A definite loop
using a for fruit = 'banana'
for letter in fruit :
statement is much print letter b
more elegant a
n
• The iteration a
variable is n
index = 0 a
completely taken while index < len(fruit) :
care of by the for letter = fruit[index]
print letter
loop index = index + 1
• This is a simple
loop that loops
through each letter
in a string and word = 'banana'
count = 0
counts the number for letter in word :
of times the loop if letter == 'a' :
count = count + 1
encounters the 'a' print count
character.
print letter
The iteration variable “iterates” though the string and the block (body)
of code is executed once for each value in the sequence
Slicing Strings
Module Code and Module Title Title of Slides
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
of the string
respectively
Slicing Strings
Module Code and Module Title Title of Slides
String Concatenation
• When the +
operator is >>> a = 'Hello'
applied to >>> b = a + 'There'
>>> print b
strings, it HelloThere
means >>> c = a + ' ' + 'There'
>>> print c
"concatenation" Hello There
>>>
http://docs.python.org/lib/string-methods.html
Module Code and Module Title Title of Slides
http://docs.python.org/lib/string-methods.html
Module Code and Module Title Title of Slides
String Library
http://docs.python.org/lib/string-methods.html
Module Code and Module Title Title of Slides
Searching a
String
• We use the find() b a n a n a
function to search for a
substring within another 0 1 2 3 4 5
string
• find() finds the first
occurance of the
>>> fruit = 'banana'
substring
>>> pos = fruit.find('na')
• If the substring is not >>> print pos
found, find() returns -1 2
• Remember that string >>> aa = fruit.find('z')
position starts at zero >>> print aa
-1
• The replace()
function is like a
“search and
replace” operation
in a word >>> greet = 'Hello Bob'
>>> nstr = greet.replace('Bob','Jane')
processor
>>> print nstr
• It replaces all Hello Jane
occurrences of the >>> nstr = greet.replace('o','X')
search string with >>> print nstrHellX BXb
the replacement >>>
string
• Sometimes we want
to take a string and
remove whitespace at
>>> greet = ' Hello Bob '
the beginning and/or >>> greet.lstrip()
end 'Hello Bob '
>>> greet.rstrip()
• lstrip() and rstrip() to ' Hello Bob'
the left and right only >>> greet.strip()
• strip() Removes both 'Hello Bob'
>>>
begin and ending
whitespace
new_list = ['John','Cat','Marry','Gold']
name = input("Enter a name to search for ")
for item in new_list:
if (item == name):
print(item)