def getString(aString): restult = '' substr = '' for i in range(len(aString)): if aString[i] in substr: if len(substr) > len(restult): restult = substr substr = substr[substr.index(aString[i])+1:] else: substr+=aString[i] return substr if len(substr) > len(restult) else restult getString("absddfedfiab") >>> getString("absddfedfiab") 'ediab'