o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
Write a Python function that checks if a string is a palindrome.
- User
Write a Python function that checks if a string is a palindrome.
- Assistant
Here’s a simple Python function to check whether a given string is a palindrome. It
- normalizes case,
- strips out non-alphanumeric characters,
- and then compares the cleaned string to its reverse.
import re def is_palindrome(s: str) -> bool: """ Returns True if s is a palindrome (ignoring case & non-alphanumeric chars), else False. """ # 1. Remove non-alphanumeric characters and convert to lowercase cleaned = re.sub(r'[^A-Za-z0-9]', '', s).lower() # 2. Check if cleaned string equals its reverse return cleaned == cleaned[::-1] # Example usage tests = [ "Racecar", "A man, a plan, a canal: Panama!", "Hello, World!" ] for t in tests: print(f"'{t}' → {is_palindrome(t)}")Output:
'Racecar' → True 'A man, a plan, a canal: Panama!' → True 'Hello, World!' → FalseIf you only care about exact, case-sensitive matches (i.e. no stripping), you can simplify to:
def is_palindrome_simple(s: str) -> bool: return s == s[::-1]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.