Back to Browse

Check if a String is Palindrome or Not in Python | EP-24 Python Program to Check Palindrome

424 views
Aug 6, 2024
12:40

method 1: Reverse the string using slicing String = input("enter the string") print(String) revered_string =String[::-1] print(revered_string,"rev") if(revered_string == String): print("palindrome") else: print("not palindrome") method2: Check String is Palindrome using one extra variable """ In this method, the user takes a character of string one by one and store it in an empty variable. After storing all the characters user will compare both the string and check whether it is palindrome or not.""" x = input("enter the string"): Prompts the user to enter a string and stores it in the variable x. print(x): Prints the entered string to the console. w = " ": Initializes an empty string and assigns it to the variable w. This variable will be used to store the reversed string. for i in x:: Starts a for loop that iterates through each character i in the input string x. w = i + w: Appends the current character i to the beginning of the w string. This effectively reverses the characters of the input string. if(w==i):: Check if the reversed string w is equal to the current character i. This is incorrect logic when checking a palindrome. print("palindrome"): Prints "palindrome" if the incorrect condition in the previous line is true. else:: If the condition is false (which it always will be), execute the following block. print("not palindrome"): Prints "not palindrome" if the condition is false. #python #programming #tutorial #palindrome #coding #pythonprogramming #pythoncode #pythontutorial #learnpython #codingtutorial #datascience #algorithms #datastructures #beginner #intermediate #codingchallenge #pythonchallenge #PythonTutorial #PalindromeChecker #PythonBeginners #TechEducation

Download

0 formats

No download links available.

Check if a String is Palindrome or Not in Python | EP-24 Python Program to Check Palindrome | NatokHD