Friday, February 5, 2021

Python program for reversing a string

First Method:


string = input("Enter a string:")

r = ""

l = len(string)

for a in range(l):

    r = r + string[l-a-1]

if(string == r):

    print(string, "is a palindrome")

else:

    print(string, "is not a palindrome")


Second Method: 


string = input("Enter a string:")

r = ""

length = len(string)

for a in range(-1, (-length-1), -1):

    r = r + string[a]

if(string == r):

    print(string, "is a palindrome")

else:

    print(string, "is not a palindrome")



No comments:

Post a Comment

How to Fight Overthinking or Negative Thoughts?

How to Combat Overthinking or Negative Thoughts? 1. Don’t Fight Your Thoughts Trying to suppress or control negative thoughts often backfire...