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

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 programe to find the factorial of a number

 Python program to find the factorial of a number Factorial number of a number is a product from 1 to the number which is from 1 to n where n is the number Example : factorial of 5 = 1*2*3*4*5 = 120 Code: #function to find factorial number def facto(n):     factorial = 1     for i in range(1,n+1):         factorial = factorial * i     return factorial #Take the input from the user and store in n n = int(input()) if n == 0: #factorial of 0 is always 1     print(1) else:     print(facto(n)) #other than 0 we have to find the factorial number by calling the function Input : 5 Output : 120 There is another method to find factorial number using recursion. Feel free to check the code in a code editor and try it out and also i am welcome to any suggestion. Here is the previous post : https://shotwithcode.blogspot.com/2020/12/python-automation-with-sele...

Python automation with selenium.

Automate with selenium In this blog we are going to see how to automate some task with selenium. Selenium is an opensource framework which is used for testing web application by scrapping into the web site. To apply selenium in python for testing an application we need to follow some step. So in these this blog we are going to explain how to use selenium to go to the google and search for shotwithcode blog and read the blog. Step1: using pip in python we have to install selenium "pip install selenium" Step2: We need a web driver in which it will run the browser and tell the browser what to do. So if we use chrome we need to download chrome driver else if we use other browser then we need to download the web driver for that browser You can download the Chrome web driver here https://chromedriver.chromium.org/downloads Remember to download the chrome driver related to the chrome version we have Step3:After download the driver extract the file and save into a location which is e...