欢迎访问宙启技术站
智能推送

编写一个Python函数来检查一个字符串是否是回文。

发布时间:2023-08-12 14:41:01

def is_palindrome(string):

    """

    This function checks if a string is a palindrome.

    

    Parameters:

    string (str): The string to be checked.

    

    Returns:

    bool: True if the string is a palindrome, False otherwise.

    """

    

    # Convert the string to lowercase and remove any whitespace

    string = string.lower().replace(" ", "")

    

    # Reverse the string

    reversed_string = string[::-1]

    

    # Check if the original string is equal to the reversed string

    if string == reversed_string:

        return True

    else:

        return False

# Testing the function

string1 = "racecar"

print(is_palindrome(string1))   # True

string2 = "hello world"

print(is_palindrome(string2))   # False

string3 = "A man a plan a canal Panama"

print(is_palindrome(string3))   # True

string4 = "madam"

print(is_palindrome(string4))   # True