Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

12.3 Recursion with strings and lists

Learning objectives

By the end of this section you should be able to

  • Demonstrate the use of recursion to solve a string problem.
  • Demonstrate the use of recursion to solve a list problem.
  • Use the built-in count list function.

Recursion with strings

A word that is spelled the same forward and backward is called a palindrome. Ex: racecar.

Recursion can be used to identify whether a given word is a palindrome.

Recursion with lists

The animation below shows a recursive way to check whether two lists contain the same items but in different order.

The count function returns a count of the number of items in a list that match the given item, and returns 0 otherwise. Ex: For list_num = [1, 3, 3, 4], list_num.count(3) returns 2.