{"id":6927,"date":"2023-10-31T08:16:01","date_gmt":"2023-10-31T02:46:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=6927"},"modified":"2023-11-10T12:08:42","modified_gmt":"2023-11-10T06:38:42","slug":"python-program-to-check-if-a-string-is-palindrome-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-string-is-palindrome-or-not\/","title":{"rendered":"Python program to check if a string is palindrome or not"},"content":{"rendered":"
Palindrome:<\/strong><\/p>\n If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome!<\/p>\n Examples for string palindrome<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Examples for number palindrome<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:\u00a0<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n There are several ways to check whether the given number is palindrome or not some of them are:<\/p>\n Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Algorithm:<\/strong><\/p>\n Below is the implementation:<\/b><\/p>\n Output:<\/strong><\/p>\n Approach:\u00a0<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:\u00a0<\/strong><\/p>\n Below is the Implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Palindrome: If a number reads the same both forward and backward, it is called a Palindrome number. And the insane part is that it doesn’t just apply to numbers. Even if a string reads the same in both directions, it is always a Palindrome! Examples for string palindrome Example 1: Input:\u00a0 given_string = madam Output: …<\/p>\ngiven_string = madam<\/pre>\n
The given string madam is palindrome<\/pre>\n
Here the madam word reads same from front and end so it is palindrome<\/pre>\n
given_string = vicky<\/pre>\n
The given string vicky is not palindrome<\/pre>\n
Here vicky and ykciv are not equal so it is not a palindrome<\/pre>\n
given_number =12321<\/pre>\n
The given number 12321 is palindrome<\/pre>\n
Here the number 12321 reads same from front and end so it is palindrome<\/pre>\n
given_number =12345<\/pre>\n
The given number 12345 is not palindrome<\/pre>\n
Here 12345 and 54321 are not equal so it is not a palindrome<\/pre>\n
Checking Palindrome in Python<\/h2>\n
\n
Method #1:Using while loop to check whether the number is palindrome<\/h3>\n
\n
# given number\r\ngiven_num = 12321\r\n# taking another variable to store the copy of original number\r\n# and initialize it with given num\r\nduplicate_num = given_num\r\n# Take a variable reverse_number and initialize it to null\r\nreverse_number = 0\r\n# using while loop to reverse the given number\r\nwhile (given_num > 0):\r\n # implementing the algorithm\r\n # getting the last digit\r\n remainder = given_num % 10\r\n reverse_number = (reverse_number * 10) + remainder\r\n given_num = given_num \/\/ 10\r\n# if duplicate_num and reverse_number are equal then it is palindrome\r\nif(duplicate_num == reverse_number):\r\n print(\"The given number\", duplicate_num, \"is palindrome\")\r\nelse:\r\n print(\"The given number\", duplicate_num, \"is not palindrome\")\r\n<\/pre>\n
The given number 12321 is palindrome<\/pre>\n
Method #2: Using for loop and string concatenation to check whether the number is palindrome<\/h3>\n
\n
# given number\r\ngiven_num = 12321\r\n# taking empty string\r\nreverse_string = \"\"\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# calculating the length of string\r\nlength = len(strnum)\r\n# Traverse the strnum string in reverse order using for loop range function\r\nfor index in range(length-1, -1, -1):\r\n # add the character to reverse_string using string concatenation\r\n reverse_string = reverse_string+strnum[index]\r\n# if reverse_string and strnum are equal then it is palindrome\r\nif(reverse_string == strnum):\r\n print(\"The given number\", given_num, \"is palindrome\")\r\nelse:\r\n print(\"The given number\", given_num, \"is not palindrome\")\r\n<\/pre>\n
The given number 12321 is palindrome<\/pre>\n
Method #3:Using for loop and string concatenation to check whether the string is palindrome<\/h3>\n
\n
# given string\r\ngiven_string = \"madam\"\r\n# taking empty string\r\nreverse_string = \"\"\r\n# calculating the length of string\r\nlength = len(given_string)\r\n# Traverse the strnum string in reverse order using for loop range function\r\nfor index in range(length-1, -1, -1):\r\n # add the character to reverse_string using string concatenation\r\n reverse_string = reverse_string+given_string[index]\r\n# if reverse_string and strnum are equal then it is palindrome\r\nif(reverse_string == given_string):\r\n print(\"The given string\", given_string, \"is palindrome\")\r\nelse:\r\n print(\"The given string\", given_string, \"is not palindrome\")\r\n<\/pre>\n
The given string madam is palindrome<\/pre>\n
Method #4:Using slicing to check whether the number is palindrome<\/h3>\n
\n
# given number\r\ngiven_num = 12321\r\n# Convert the given_num to string using str\r\nstrnum = str(given_num)\r\n# calculating the length of string\r\nlength = len(strnum)\r\n# Reversing the string using slicing\r\nreverse_string = strnum[len(strnum)::-1]\r\n# if reverse_string and strnum are equal then it is palindrome\r\nif(reverse_string == strnum):\r\n print(\"The given number\", given_num, \"is palindrome\")\r\nelse:\r\n print(\"The given number\", given_num, \"is not palindrome\")\r\n<\/pre>\n
The given number 12321 is palindrome<\/pre>\n
Method #5: Using slicing to check whether the string is palindrome<\/h3>\n
\n
# given string\r\ngiven_string = \"madam\"\r\n# calculating the length of string\r\nlength = len(given_string)\r\n# Reversing the string using slicing\r\nreverse_string = given_string[len(given_string)::-1]\r\n# if reverse_string and strnum are equal then it is palindrome\r\nif(reverse_string == given_string):\r\n print(\"The given string\", given_string, \"is palindrome\")\r\nelse:\r\n print(\"The given string\", given_string, \"is not palindrome\")\r\n<\/pre>\n
The given string madam is palindrome<\/pre>\n
\n