longest palindrome in a string

  

#!/bin/env python
string1="prefixsatorarepotenetoperarotassuffix"
maxlength=-1
for i in range(0,len(string1)):
        if (len(string1) - i ) <= maxlength :
                break
        for j in range(i, len(string1)):
                if ( len(string1) - j ) < maxlength :
                        break
                else:
                        string2 = string1[i:len(string1)-j+i]
                        string2 = string2[::-1]
                        if string1[i:len(string1)-j+i] == string2:
                                maxlength = len(string2)
                                finalstring = string2
                                break
print finalstring


About this entry