Iterative Palindrome Function - Python Programming Exercise

In this exercise, you will develop a Python program with an iterative function to check if a string is a palindrome (symmetric). This exercise is perfect for practicing function definition, loops, and string manipulation in Python. By implementing this function, you will gain hands-on experience in handling function definitions, loops, and string manipulation in Python. This exercise not only reinforces your understanding of functions but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 Exercise

Iterative Palindrome Function

 Objective

Develop a Python program with an iterative function to check if a string is a palindrome (symmetric). For instance, "RADAR" is considered a palindrome.

 Example Python Exercise

 Copy Python Code
# Function to check if a string is a palindrome using iteration
def is_palindrome(s):
    # Convert the string to lowercase to make the check case-insensitive
    s = s.lower()

    # Loop through the string from both ends towards the middle
    for i in range(len(s) // 2):
        # Compare characters at the start and end positions
        if s[i] != s[len(s) - i - 1]:
            return False  # Return False if characters don't match

    return True  # Return True if the string is a palindrome

# Example strings to test
test_strings = ["RADAR", "hello", "madam", "world"]

# Check if each string is a palindrome
for test_string in test_strings:
    result = is_palindrome(test_string)
    print(f"Is '{test_string}' a palindrome? {result}")

 Output

python palindrome_check.py
Is 'RADAR' a palindrome? True
Is 'hello' a palindrome? False
Is 'madam' a palindrome? True
Is 'world' a palindrome? False

 Share this Python Exercise

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Recursive Palindrome Function

    In this exercise, you will develop a Python program with a recursive function to determine if a string is symmetric (a palindrome). This exercise is perfect fo...

  •  Function FindMinMax

    In this exercise, you will develop a Python program with a function called "FindRange", where the user is prompted to input a minimum value (a number) and a maximum v...

  •  Function Multiply & MultiplyRecursive

    In this exercise, you will develop a Python program with two functions, Multiply and MultiplyRecursive, to compute the product of two numbers by using addition. The f...

  •  Functions: Hello and Goodbye

    In this exercise, you will develop a Python program where the main function should look like this: SayHello() and SayGoodbye(). You must define the functions SayHello...

  •  Parameterized Function

    In this exercise, you will develop a Python program where the main function should look like this: SayHello("John") and SayGoodbye(). You must define the functions Sa...

  •  Value-Returning Function

    In this exercise, you will develop a Python program where the main function should define and call a sum function that accepts two integers as parameters and returns ...