YouTip LogoYouTip

Python Palindrome String

## Python Program to Check if a String is a Palindrome A **palindrome** is a string that reads the same backward as forward, such as `"madam"`, `"racecar"`, or `"radar"`. In Python, checking whether a string is a palindrome is a classic programming exercise that can be solved efficiently using several approaches, ranging from simple string slicing to two-pointer techniques. This tutorial will guide you through the most common and optimized ways to check for palindromes in Python. --- ### Understanding the Logic To determine if a string is a palindrome, we typically perform the following steps: 1. **Normalization**: Convert the string to lowercase and remove non-alphanumeric characters (like spaces and punctuation) so that the check is case-insensitive and ignores formatting. 2. **Comparison**: Compare the normalized string with its reversed version. If they are identical, the string is a palindrome. --- ### Method 1: Using Python String Slicing (Recommended) The most Pythonic and concise way to reverse a string is by using slice notation `[::-1]`. This method is highly optimized in CPython. #### Code Example ```python def is_palindrome(s): # Convert the string to lowercase and remove spaces s = s.lower().replace(" ", "") # Compare the string with its reversed version return s == s[::-1] # Test the function test_string = "A man a plan a canal Panama" result = is_palindrome(test_string) print(f"Is '{test_string}' a palindrome? {result}") ``` #### Code Explanation 1. **`s.lower().replace(" ", "")`**: Converts the entire string to lowercase and removes all spaces. This ensures that phrases like `"A man a plan..."` are evaluated correctly regardless of casing and spacing. 2. **`s[::-1]`**: This is Python's slicing syntax. Specifying `-1` as the step slice reverses the string. 3. **`return s == s[::-1]`**: Compares the processed string with its reversed counterpart. It returns `True` if they match, and `False` otherwise. #### Output ```text Is 'A man a plan a canal Panama' a palindrome? True ``` --- ### Method 2: Handling Punctuation and Special Characters Real-world strings often contain punctuation marks (like commas, periods, or hyphens). To make your palindrome checker robust, you can filter out all non-alphanumeric characters using Python's built-in `str.isalnum()` method. #### Code Example ```python def is_palindrome_advanced(s): # Keep only alphanumeric characters and convert to lowercase cleaned_chars = [char.lower() for char in s if char.isalnum()] cleaned_str = "".join(cleaned_chars) # Check if the cleaned string is equal to its reverse return cleaned_str == cleaned_str[::-1] # Test cases print(is_palindrome_advanced("No 'x' in Nixon")) # True print(is_palindrome_advanced("Was it a car or a cat I saw?")) # True print(is_palindrome_advanced("Hello, World!")) # False ``` #### Output ```text True True False ``` --- ### Method 3: The Two-Pointer Approach (Memory Efficient) If you are working with extremely large strings and want to avoid creating a reversed copy of the string in memory, you can use the **Two-Pointer Approach**. This method compares characters from both ends moving toward the center. #### Code Example ```python def is_palindrome_two_pointer(s): # Clean the string first s = "".join(char.lower() for char in s if char.isalnum()) left = 0 right = len(s) - 1 while left < right: if s != s: return False left += 1 right -= 1 return True # Test the function print(is_palindrome_two_pointer("racecar")) # True print(is_palindrome_two_pointer("python")) # False ``` --- ### Summary of Approaches | Method | Time Complexity | Space Complexity | Best Used For | | :--- | :--- | :--- | :--- | | **Slicing (`[::-1]`)** | $O(N)$ | $O(N)$ | Standard use cases, clean and readable code. | | **List Comprehension + Slicing** | $O(N)$ | $O(N)$ | Sentences containing punctuation and mixed casing. | | **Two-Pointer Approach** | $O(N)$ | $O(1)$ (excluding cleaning step) | Memory-optimized scenarios and technical interviews. |
← Python Person ClassPython Recursive Sum β†’