# ref: Zybooks Chapter 7.9 Lab: Palindrome # simple is_palindrome. def is_parlindrome_1(s): return s == s[::-1] # using an alternative method. def is_parlindrome_2(s): return s == "".join(reversed(s)) s1 = "racecar" s2 = "Racecar" s3 = "Hello world" s4 = "A Toyota's a Toyota." print(f"s1: {s1}") print(f"s2: {s2}") print(f"s3: {s3}") print(f"s4: {s4}") print(f"is_parlindrome_1(s1): {is_parlindrome_1(s1)}") print(f"is_parlindrome_1(s2): {is_parlindrome_1(s2)}") print(f"is_parlindrome_1(s3): {is_parlindrome_1(s3)}") print(f"is_parlindrome_1(s4): {is_parlindrome_1(s4)}") print(f"is_parlindrome_2(s1): {is_parlindrome_2(s1)}") print(f"is_parlindrome_2(s2): {is_parlindrome_2(s2)}") print(f"is_parlindrome_2(s3): {is_parlindrome_2(s3)}") print(f"is_parlindrome_2(s4): {is_parlindrome_2(s4)}") def is_parlindrome_3(s): # case insensitive return s.lower() == s[::-1].lower() print(f"is_parlindrome_3(s1): {is_parlindrome_3(s1)}") print(f"is_parlindrome_3(s2): {is_parlindrome_3(s2)}") print(f"is_parlindrome_3(s3): {is_parlindrome_3(s3)}") print(f"is_parlindrome_3(s4): {is_parlindrome_3(s4)}") def only_alphabet(s): # return only alphabets in s. result = '' for char in s: if char.isalpha(): result += char return result def only_alphabet_2(s): # return only alphabets in s. return "".join(char for char in s if char.isalpha()) def is_parlindrome_4(s): # case insensitive and remove non alphabetic characters. s = only_alphabet(s).lower() return s == s[::-1] print(f"is_parlindrome_4(s1): {is_parlindrome_4(s1)}") print(f"is_parlindrome_4(s2): {is_parlindrome_4(s2)}") print(f"is_parlindrome_4(s3): {is_parlindrome_4(s3)}") print(f"is_parlindrome_4(s4): {is_parlindrome_4(s4)}")