Skip to content

Latest commit

 

History

History
45 lines (27 loc) · 1.49 KB

File metadata and controls

45 lines (27 loc) · 1.49 KB

9.1 Check Palindrome

The problem

Check whether the string is a palindrome.

Hint

A palindrome is a word, phrase, or sequence that reads the same backward as forwards. For example, the word- madam

If you read it backward it will be madam as well. Similarly, eye, rotor, kayak, racecar, level, etc. are palindromes.

To check a palindrome, just reverse the word or the phrase, and then check whether the reversed string is the same as the initial string.

If both are the same, it will be a palindrome.

Solution

my_str = input('String to check: ')
rev_str = my_str[::-1]

if my_str == rev_str:
  print("It is palindrome")
else:
  print("It is not palindrome")

Try it on Programming Hero

Explanation

We used the shortcut method to get the reversed string. And then we compared using if-else.

That's it.

The problem might sound complicated at the beginning. However, if you know the technique, the solution could become easier.

So, the more you explore, the more you try, the more shortcut trick you will master.

  Next Page  

tags: programming-hero python python3 problem-solving programming coding-challenge interview learn-python python-tutorial programming-exercises programming-challenges programming-fundamentals programming-contest python-coding-challenges python-problem-solving