D
D
daredaredare2018-08-04 11:29:08
Python
daredaredare, 2018-08-04 11:29:08

Python. How to check if a word is a palindrome by deleting one letter?

I have, for example, the string sevens and with .replace("n"," ") print(str[::-1]) I get a palindrome.
But I want to create a universal code that can work with any introductory words and check them for a palindrome. My idea was to make two loops for an even length of the string and for an odd one: for an even length, I compare both halves of the text and remove a character that is not repeated in the other part of the string, for an odd length, I split the string by the middle character and compare both parts again, after I take everything out in reverse order. But I can't implement it, because I just started writing code and don't know much yet.
I will be glad for any help - find errors in my code, add it or send an example of your code.

str = input()

a = len(str)
while (len(str)%2) == 0:
        print(str[::-1])
        break
while (len(str)%2) == 1:
        print(str[::-1])
        break

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Spiridonov, 2018-08-04
@customtema

return (word == word[::-1])
[::-1] will reverse the string
(word == word[::-1]) will compare the string with its reversed version
return will return a boolean result

A
albertalexandrov, 2018-08-04
@albertalexandrov

Solved the problem of finding the longest palindrome as a test task when applying for a job. Maybe it will be useful https://gitlab.com/albertalexandrov/test-tasks-for...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question