Palindrome code in Python

Certainly! Here's an example of a palindrome code in Python:

def is_palindrome(word):
    # Convert the word to lowercase and remove any spaces
    word = word.lower().replace(" ", "")

    # Check if the word is equal to its reverse
    if word == word[::-1]:
        return True
    else:
        return False

# Test the function
word = input("Enter a word: ")
if is_palindrome(word):
    print("It's a palindrome!")
else:
    print("It's not a palindrome.")

In this code, we define a function called is_palindrome that takes a word as input. The function first converts the word to lowercase and removes any spaces using the lower() and replace() string methods, respectively. Then, it checks if the word is equal to its reverse by comparing it with word[::-1], which uses slicing to reverse the word.

To test the function, we prompt the user to enter a word and then call the is_palindrome function. If the word is a palindrome, it prints "It's a palindrome!" Otherwise, it prints "It's not a palindrome."

댓글

이 블로그의 인기 게시물

Delete topic on Kafka, 카프카 토픽 삭제하기

how to delete all issues on project in sentry, 센트리 이슈 삭제하기

ftplib.error_perm: 550 Permission denied

Pyspark 를 사용한 테이블 조인, table join with pyspark

[OS_Linux_Ubuntu] telnet & ssh 설치와 설정 (installation / configuration)

10 Python Functions Every Data Scientist Must Memorize

Python program to convert an .xlsx file to JSON using the openpyxl library

Read a file in Java, 자바 파일 읽기

Auto-populate a calendar in an MUI (Material-UI) TextField component