Skip to main content

Python program to check if a number or string a palindrome or not

Given a string or an integer,Write a python program to check if a string or an integer is a palindrome.

Palindrome

A given string or a number is said to be palindrome if the opposite or the reverse of the string or a number is same as the orginal string or number.

Example:

1. 12321 - Palindrome

2. 345 - Not Palindrome

3. SOS - Palindrome

4. RUN - Not Palindrome

Python Program

#Function if the given input is a string
def palinStr(n):
    #check if the reverse of a string is equal to the original string
    print('Palindrome') if n == n[::-1] else print('Not a palindrome')
#Function if the given input is a number
def palinInt(n):
    rem = 0
    temp = n
    while n>0:
        #Taking the last number
        dig = n%10
        #reverse the number from last digit to first digit
        rem = rem*10+dig
        n = n//10
    #check if the reverse of a string is equal to the original string
    print("Palindrome") if temp == rem else print("Not a palindrome")

#Taking user input
n = input()
#calling the function base on the user input, A string or a number
palinInt(int(n)) if n.isnumeric() else palinStr(n)

Input:

12321

Output:

Palindrome

Input:

RUN

Output:

Not a palindrome

Comments

Popular posts from this blog

I built a Django weather app using openweatherapp API

 I built a Django weather app using openweatherapp API.  Yes i built this simple weather app using openweatherapp API, it was fun an easy to built with django. How do i built it? I install the module needed for this project, the only module we need is a request module for python since i am using python to built this project. After installing and setting up the project with django i download a template and start with creating the views for the project. Here how goes: 1.First go to openweather app website : https://openweathermap.org/ 2.Signup for an account so that the openweather app will send you an api key 3.After singup with an account go to API and select the API you want to use 4.In an API doc it will attach a link which look like this api.openweathermap.org/data/2.5/weather?q= {city name} &appid= {API key} So we need to replace the {API key} with our own API key. 5.That it for the API so in order to make a request or to call the API in python we use a request module...

Best office computer accesories

 Here are some of the best computer accesories for your office at an affordable price in India. 1. Oxyura Gaming Mouse Pad Click here to see the Mouse pad https://amzn.to/3pVY8KL 2. Zebronics Zeb-Alex Wired Usb optical mouse Click here to see and buy the Mouse https://amzn.to/3ILT2t6 3. Usb wired keyboard Click here to see and buy the keyboard https://amzn.to/3mmzjqv 4.Laptop stand Click here to see and buy the laptop stand https://amzn.to/3dTgkih All link are from amazon so go ahead and check it out it is at an afordable price.

Python program to check whether the given number is an armstrong or not.

  Given an integer,Write a python program to check if an integer is an Armstrong or not. Armstrong A given a number is said to be armstrong if the number of order or length n is digit*n times and the sum is equal to the number given example - 153 = 1*1*1 + 5*5*5 +3*3*3 Example: 1. 153 - Armstrong 2. 345 - Not armstrong 3. 1634 - Armstrong Python Program #Check whether the given integer is an armstrong or not #function for armstrong def armstrong(n):     #convert into str to calculate the order of the digit     a = str(n)     temp,sum= n,0     while temp>0:         #modulo by 10 to get each and every digit of a number         digit = temp%10         #multiply each digit with the order of the digit and sum the values         sum+=digit**len(a) ...