For Enquiry: 93450 45466

Palindrome Program In Python



In this blog we’ll go step by step to design programs to check if a string or a number is palindrome or not .You can also create your own program to solve this problem based on the algorithms.

What is Palindrome
What is a Palindrome Number, Palindrome String or a Palindrome Phrase
Python Program To Verify A Number As Palindrome
Python Program To Verify A Palindrome String
Python Program To Verify Input As Palindrome

Note that we will be using loops, functions join and slicing methods to solve this specific problem. You can gain great insights of these topics and more about python with real-world based problems in Python Training in Chennai or Python Training in Bangalore at FITA.

Palindrome

On 02,02, 2020, it read the same in both the MM/DD/YYYY and the DD/MM/YYYY format, and at just after 2 a.m., it was 02:02:20. The next such date will come after 101 years on 12/12/2121.

Such patterns are palindrome. The palindrome is defined as the words, numbers or phrases that form mirror images on reversal.

Palindrome Number

A number is palindrome if the digits of it when reversed, turns out to be the original number.For eg: 12321 or 123321 have the same mirror images individually.

Palindrome String

The string or the word aibohphobia is an example of a palindrome string, which literally means, an irritable fear of palindromes.

A palindrome phrase is just another collection of letters+numbers which reads exactly as its reversed version.

Program 1: Python Program To Verify A Palindrome Number

Algorithm:

  1. Take the input and store in a variable.
  2. Use a temporary variable with the same input value for reserve.
  3. Use a while loop to store each digit (using modulus) and reverse it (multiply by 10 and add the removed digit to the current reversed number).
  4. Remove the last digit from the input.
  5. The loop terminates when the input is reduced to zero or becomes zero.
  6. The reversed number is then compared with the input value stored in the temporary variable.
  7. If both are equal, then the input is a palindrome number, else it is not.
  8. Print the final result.

nmb = int(input(“Give a number to check if palindrome:\n”))

tmp = nmb

revsd = 0

while (nmb > 0):

digit = nmb% 10

rev = rev * 10 + digit

nmb= nmb// 10

if (tmp == revsd):

print(“The number is palindrome!”)

else:

print(“Not a palindrome!”)

Output:

Give a number to check if palindrome:

1231

Not a palindrome!

Program 2: Python Program To Verify A Palindrome String
Algorithm:

  1. Define a function with an argument.
  2. Run a for loop, from 0 to the length of the input divided by 2.
    • Note that if the string has an odd number of characters the middlemost term will be ignored.
  3. Compare the first with last, second with second from the last and so on.
  4. If any compare does not match then it is not a palindrome, and return False.
  5. Return true if all of the comparisons match.

def Palindrome(string):

# Run loop from 0 to half of the input

for i in range(0, int(len(string) / 2)):

if string[i] != str[len(string) – i – 1]:

return False

return True

s = input(“Give me your word:\n”)

if Palindrome(s):

print(“It is A Palindrome”)

else:

print(“It is not a Palindrome”)

Output:

Give me your word:

madam

It is A Palindrome

Program 3: Python Program To Verify Input As PalindromeAnd now, let’s solve this problem in a more pythonic way.

Algorithm:

  1. Take input as either a word or number.
    • Note that the number will be taken as a string using the input method.
  2. Reverse it using the string slicing method.
  3. Print the statement based on the equality of the reversed and the original word or number.

string = input(‘Enter word or number:\n’)

print(‘A Palindrome’ if string == string[::-1] else “Not a palindrome!”)

Output:

Enter word or number:

Palindrome

Not a Palindrome!

You can also try to solve this problem using the built-in reversed and join function of Python…

To get in-depth knowledge of Python along with its various applications, you can enroll for live Python Online Training with certification, support and career guidance.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai, Bangalore & Online

  93450 45466

Coimbatore

 95978 88270

Pondicherry

93635 21112

Madurai

97900 94102

Tirupur

99401 22502

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

Read More Read less
  • Are You Located in Any of these Areas

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!