DAY 7 of PYTHON top 100 questions : from Basic to Advanced !!

DAY 7 of PYTHON top 100 questions : from Basic to Advanced !!

Write a Python program to check whether a given string is a palindrome or not:-

str=input("ENTER THE STRING : ")
if str == str[::-1]:
    print("YES")
else:
    print("NO")

Output:

ENTER THE STRING : eye
YES
  • The code first prompts the user to input a string using the input() function and assigns it to the variable str.

  • The [::-1] slice notation is used to reverse the string. This creates a new string that has the same characters as the original string but in reverse order.

  • The == operator is used to compare the original string with its reversed version. If they are the same, the string is a palindrome and the code prints "YES". If they are different, the string is not a palindrome and the code prints "NO".


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!