[Tutor] Code for checking whether an input string is a palindromeor not.

Alan Gauld alan.gauld at btinternet.com
Fri Oct 13 15:08:48 CEST 2006


"Asrarahmed Kadri" <ajkadri at googlemail.com> wrote in message

> Here is a code that reports whether the string is a palindrome or 
> not.

I'd suggest a simpler approach is to use the string/list methods in 
Python:

string1 = list(raw_input("enter a string\n"))  #using a list is easier
string2 = string1[:]  # make a copy
string2.reverse()
if string1 == string2:
     print "The entered string is a PALINDROME"
else:
     print "The entered string is not a PALINDROME"

HTH,

Alan G

> string1 = raw_input("enter a string\n")
> str_len = len(string1)
> flag = 0
> j = str_len-1
>
> for i in range(0,(str_len-1)/2):
> if string1[i] == string1[j]:
>  j = j-1
>
> else:
>  flag = 1
>  break
> if flag ==0:
>     print "The entered string is a PALINDROME"
> else:
>     print "The entered string is not a PALINDROME"

--------------------------------------------------------------------------------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list