Lab 4

Objectives:

  • Practice with strings and loops

Palindrome detection

Write a function called is_palindrome that determines if a given string is a palindrome, i.e. is the same as its reverse. Here are some examples:

>>>is_palindrome('abba')      
True
>>> is_palindrome('abab')
False
>>> is_palindrome('tenet')
True
>>> is_palindrome('banana')
False
>>> is_palindrome('straw warts')
True

Substring counting

Write a function called count(s, substr) that returns how many times the string substr occurs in s. Here are some examples of the output your function should produce:

>>> count('is', 'Mississippi')      
2
>>> count('an', 'banana')
2
>>> count('ana', 'banana')
2
>>> count('nana', 'banana')
1
>>> count('nanan', 'banana')
0

String reversal

Write a function called reverse that reverses a string. Here are some examples of the output your function should produce:

>>> reverse('happy')
'yppah'
>>> reverse('Python')
'nohtyP'
>>> reverse("")
''
>>> reverse("P")
'P'