Palindrome Program in Python using for loop

1 comment

In this program, we are going to check whether the number or string is a palindrome or not in python, using for loop and also using reversed() function.


    Palindrome program in python for numbers using for loop

    number=input("Enter any number :")
    i=0
    for i in range(len(number)):
        if number[i]!=number[-1-i]:
            print('It is not a palindrome')
            break
        else:
            print('It is a palindrome')
            break

    Output:-

    Enter any number:5665
    It is a palindrome

    Palindrome program in python using string

    word=input("Enter any word :")
    rev=reversed(word)
    if list(word)==list(rev):
        print('It is a palindrome')
    else:print('It is not a palindrome')
    Output:-
    Enter any word: madam
    It is a palindrome

    Approach for Palindrome program in python using string:

    • First, we input the string using the input() function.
    • Using the reversed() function we reverse the word entered by the user and assigned it to a new variable called rev.
    • Using the if statement, we compare the list(word) & list(rev) if they are the same then we will print "it is a palindrome" and if they are not the same then we will print "it is not a palindrome".

    Palindrome program in python for numbers using reversed() function

    number=input("Enter any number :")
    rev=reversed(number)
    if list(number)==list(rev):
        print('It is a palindrome')
    else:print('It is not a palindrome')
    Output:-
    Enter any number:7887
    It is a palindrome

    Approach for Palindrome program in python for numbers using reversed() function:-

    • First, we input the string or number using the input() function.
    • Using the reversed() function we reverse the number or word entered by the user and assigned it to a new variable called rev.
    • Using the if statement, we compare the list(number or word) & list(rev) if they are the same then we will print it is a palindrome and if they are not the same then we will print it is not a palindrome.
    • Be careful, while writing the first statement of if statement four spaces or tab should be used.

    Q. What is a palindrome number in python?
    A number is a palindrome when its reverse is also the same as the original number.
    To check whether a number is palindrome or not, we have to reverse the number using the reversed() function. Then we compare them using if statement.
    for example:8558 is a palindrome

    Q. How do you write a palindrome program in python using for loop?
    First, we input a number using the input() function, then we reverse it using the reversed() function, and then we compare the number or a word with its reverse if they are the same then we print the number or a word entered is a palindrome & if they are not the same then we print the number or a word entered is not a palindrome.

     
    About this Palindrome Program in Python using for loop

    This is my first python program so please share and comment below to support us. You can also learn python and many more other languages from progate app, you can download it via google playstore.  

    If you have any queries related to this program you can comment below or you can also contact us also. Thanks for reading this Palindrome program in python. I hope you enjoy my programs as much as I enjoy offering them to you. If you have any questions or queries related to any program, please don't hesitate to contact me.

    If you have any queries related to this program you can comment below or you can also contact us also. Thanks for reading this Post.

     STAY SAFE, STAY CONNECTED WITH US.

    Siddharth Jha
    • Software Developer • Website Developer • Masters in SEO • Social Media Manager

    Related Posts

    1 comment

    1. How can i make palindrome program in python using while loop

      ReplyDelete

    Post a Comment

    Follow by Email